虽然是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. mvp(2)一个简单示例,加深理解

    参考: http://www.cnblogs.com/liuling/p/mvp-pattern-android.html 架构图: 1.View层 public interface NewsView ...

  2. Android OTA 升级之三:生成recovery.img

    Android OTA 升级之三:生成recovery.img 作者: 宋立新 Email:zjujoe@yahoo.com 前言 得到了ota升级包后,我们就可以用它来升级系统了.Android 手 ...

  3. 【HDOJ】3901 Wildcard

    1. 题目描述有两个长度为$10^5$的字符串,其中一个仅包含小写字母,称之为源串:另一个包含?与*通配符的模式串,并且通配符的个数不超过10.求模式串是否能匹配源串? 2. 基本思路这其实是一道Re ...

  4. 【转载】Redis多实例及分区

    主要看的这篇文章 http://mt.sohu.com/20160523/n451048025.shtml edis Partitioning即Redis分区,简单的说就是将数据分布到不同的redis ...

  5. Java里面instanceof怎么实现的

    开始完全一头雾水呀,后面看了Java指令集的介绍,逐渐理解了. https://www.zhihu.com/question/21574535/answer/18998914 下面这个答案比较直白 你 ...

  6. Android App接入微信开放平台注意事项

    一.Android第三方应用接入微信开放平台的注意事项: 1. 到微信开放平台官网申请正式的AppID(需通过审核),要填写包名.app签名的md5值.至于如何获取app签名信息,官方提供签名包apk ...

  7. Eclipse 上安装 Maven3插件

    原文:http://www.cnblogs.com/quanyongan/archive/2013/04/18/3028181.html eclipse 安装插件的方式最常见的有两种: 1. 一种是在 ...

  8. BZOJ 1123 BLO

    tarjan求割点计算答案.注意不是每一棵子树都算答案.开个变量记一下. #include<iostream> #include<cstdio> #include<cst ...

  9. Wireshark基本介绍和学习TCP三次握手(转)

    http://www.cnblogs.com/TankXiao/archive/2012/10/10/2711777.html 之前写过一篇博客:用 Fiddler 来调试HTTP,HTTPS. 这篇 ...

  10. 【英语】Bingo口语笔记(50) - Drop系列