虽然是easy,却是比较繁琐的一道题,需要考虑各种边界条件。在WA了好几遍之后,才把各种边界条件给补全。需要考虑到的因素如下:

  1. 输入不合法字符,非"0-9",对于首位,合法字符还包括"+"和"-"来代表正负号;
  2. 最前面允许出现很多个空格,即"      56";
  3. 当中间出现不合法字符,输出该不合法字符前面有效字符,如"+56a123",输出就是56;
  4. 注意越界的情况。

  当注意了上述情况之后,就没有太大问题了:

 public class Solution {
public int myAtoi(String str) {
if (str.length() == 0) return 0;
int index = 0;
//前面是空格的情况
while (str.charAt(index) == ' ') index++;
char c0 = str.charAt(index++);
long num = c0 < '0' || c0 > '9'? 0 : c0 - '0';
int flag = 1;
//记录正负号。
if (c0 == '-') flag = -1;
//判断首位的不合法字符
if ((c0 < '0' || c0 > '9') && (c0 != '+'&& c0 != '-')) return 0;
for (int i = index; i < str.length(); i++) {
char c = str.charAt(i);
//判断不合法字符,若不合法,马上返回
if (c < '0' || c > '9') {
if (flag == -1) num = -num;
return (int)num;
}
num = num * 10 + (c - '0');
//注意越界的情况
if (num > 2147483647 && flag == 1) return 2147483647;
if (num > 2147483647 && flag == -1) return -2147483648;
}
//正负号的修正
if (flag == -1) num = -num;
return (int) num;
}
}

LeetCode(8) - String to Integer (atoi)的更多相关文章

  1. 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  2. 【一天一道LeetCode】#8. String to Integer (atoi)

    一天一道LeetCode系列 (一)题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all ...

  3. 【LeetCode】8. String to Integer (atoi) 字符串转换整数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...

  4. 【LeetCode】8. String to Integer (atoi) 字符串转整数

    题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  5. 【leetcode】8. String to Integer (atoi)

    题目描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...

  6. 【LeetCode】008. String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  7. LeetCode题解 #8 String to Integer (atoi)

    又是一道恶心的简单题. 一开始没想到这么多情况的,幸好LeetCode是个很人性化的oj,能让你知道你在哪个case上错了,否则一辈子都过不了. 考虑不周到只能一个个补了. 列举一下恶心的case / ...

  8. 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 ...

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

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

随机推荐

  1. sqlserver重命名字段名称

    EXEC sp_rename 'S2BASE_PRODUCT.[PRODUCT_ID]','TABTYPE_ID','COLUMN';

  2. Open Explorer Plugin for Eclipse (eclipse 插件 在eclipse里面打开文件目录)

    就是在eclipse里面直接打开文件所在的目录地址 只要将下面的jar 文件放到你的 “$ECLIPSE_HOME/plugins”  下面,重启eclipse就ok了 要想卸载的话  停止eclip ...

  3. linq to sql ,将var 类型转为 IList 类型

    public void SOHSelecting(int startRowIndex, int maximumRows, string sortExpression, string location) ...

  4. UVa 12174 (滑动窗口) Shuffle

    首先预处理一下以每个数为结尾的前s个数是否能构成一个1~s的排列. 可以用cnt数组来记录每个数出现的次数和用一个变量记录一共有多少个不同的数出现. 然后枚举每种可能的情况,也就是枚举第一首歌会出现的 ...

  5. UVa 129 Krypton Factor【回溯】

    学习的紫书的回溯,理解起来还是好困难的说啊= = #include<iostream> #include<cstdio> #include<cstring> #in ...

  6. 重装yum

    1)卸载yum rpm -aq|grep yum|xargs rpm -e --nodeps 2)下载并安装python-urlgrabber,python-pycurl,yum-metadata-p ...

  7. BZOJ 1878 HH的项链

    不能分块(显然复杂度会炸啊.....) 离线+BIT.每个颜色在每个询问中只出现一次. #include<iostream> #include<cstdio> #include ...

  8. linux vim 配置文件(高亮+自动缩进+行号+折叠+优化)

    点评:将一下代码copy到 用户目录下 新建文件为 .vimrc保存即可生效 如果想所有用户生效 请修改 /etc/vimrc (建议先cp一份)"===================== ...

  9. USACO 2013 Nov Silver Pogo-Cow

    最近因为闲的蛋疼(停课了),所以开始做一些 USACO 的银组题.被完虐啊 TAT 貌似 Pogo-Cow 这题是 2013 Nov Silver 唯一一道可说的题目? Pogo-Cow Descri ...

  10. ecshop 调用指定分类的推荐,热卖,新品

    未测试 1.includes/lib_goods.php文件.把SQL语句改一下,与category表关联即可 将 $sql = 'SELECT g.goods_id,g.goods_name, g. ...