Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

思路:INT最大是2147483647,最小是-2147483648。所以要尤其注意-2147483648取相反数是得不到2147483648的。

class Solution {
public:
int myAtoi(string str) {
int ret = ;
int i = ;
bool pos = true; while(str[i] == ' ') i++; //neglect the spaces at the start
if(str[i] == '+') i++;
else if(str[i] == '-') {
pos = false;
i++;
} while(str[i] != '\0'){
if(str[i] > '' || str[i] <'') break; //if invalid digit, stop scanning and return the current result
if(ret > || (ret == && str[i] - '' > )){ //if overflow, return the extreme value
if(pos) return INT_MAX;
else return INT_MIN;
}
ret = ret * + str[i] - '';
i++;
}
if(pos) return ret;
else return (-ret);
}
};

8.String to Integer (atoi) (INT; Overflow)的更多相关文章

  1. 【leetcode】String to Integer (atoi)

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  2. No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  3. leetcode第八题 String to Integer (atoi) (java)

    String to Integer (atoi) time=272ms   accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...

  4. leetcode day6 -- String to Integer (atoi) &amp;&amp; Best Time to Buy and Sell Stock I II III

    1.  String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...

  5. String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )

    String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...

  6. Kotlin实现LeetCode算法题之String to Integer (atoi)

    题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...

  7. LeetCode--No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  8. leetcode-algorithms-8 String to Integer (atoi)

    leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...

  9. LeetCode: String to Integer (atoi) 解题报告

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

随机推荐

  1. appium 3-4-1034等待、日志、性能数据、xpath定位、web driver协议

    1.等待 1.1精确等待 sleep 不推荐 @Test public void testWait1() throws InterruptedException{ day_time(); Thread ...

  2. redis sentinel无法启动问题

    redis sentinel 正常启动 ./redis-sentinel sentinel.conf 就可以启动, 但是如果linux 操作系统(比如阿里云os)不支持ipv6,就会导致sentine ...

  3. 用HTML5播放IPCamera视频

    HTML5增加了vedio标签,能直接播放视频,但是播放的格式是有限的. 这里将IPCamera的视频转换OGG格式,再通过HTML5网页播放,播放还是很清晰的.IPCamera要支持RTSP等视频流 ...

  4. Hibernate JavaBean.hbm.xml配置

    主键生成策略: hibernate中必须设置主键 <generator> 由数据库维护: identity:用于自动生成主键方式(没有自增主键的数据库不使用eg:oracle) seque ...

  5. ulimit限制打开的文件数量

    以限制打开文件数为例. ulimit -Hn 查看硬限制. ulimit -Sn 查看软限制. ulimit -n 查看两个中更小的限制(软限制始终比硬限制低, 所以查看的是软限制) 设定规则 1.软 ...

  6. 关于raid5的一系列问题

    前几天我的一个同事在对计划采购的存储进行测试,期间聊到了raid5的话题,我和他的意见产生了分歧.他的说法是raid5不能挂太多盘是因为如果挂太多盘写惩罚会非常严重导致性能下降.而我的观点则是对于ra ...

  7. JavaScript中的数组和字符串

    知识内容: 1.JavaScript中的数组 2.JavaScript中的字符串 一.JavaScript中的数组 1.JavaScript中的数组是什么 数组指的是数据的有序列表,每种语言基本上都有 ...

  8. Flutter main future mirotask 的执行顺序

    下面这段代码的输出是什么? import 'dart:async'; main() { print('main #1 of 2'); scheduleMicrotask(() => print( ...

  9. 0_Simple__simpleP2P

    使用 P2P 特性在 GPU 之间传输.读写数据. ▶ 源代码.包括 P2P 使用前的各项检查,设备之间的数据互拷,主机和设备之间数据传输和相互访问. #include <stdlib.h> ...

  10. Kafka集群扩展以及重新分布分区

    我们往已经部署好的Kafka集群里面添加机器是最正常不过的需求,而且添加起来非常地方便,我们需要做的事是从已经部署好的Kafka节点中复制相应的配置文件,然后把里面的broker id修改成全局唯一的 ...