虽然是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. 如何实现上下左右键盘控制焦点使之落在相邻文本框或下拉框中-Web开发/JavaScript

    我用jquery只实现了文本框的移动(暂时上下移动等同于左右移动) $(function () { var cols = 1;//按一下跳几个控件 var obj = $("input[id ...

  2. pythonweb自动化测试

    from selenium import webdriverimport time def capture(url, save_fn="capture.png"): browser ...

  3. 如何配置LCD背光和LED,调试方法

    LCD背光和LED配置文件 alps/custom/<proj name>lk/cust_leds.c alps/custom/<proj name>/kernel/leds/ ...

  4. BZOJ 2956 模积和

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2956 题意:给出n和m.计算: 思路: i64 n,m; i64 cal(i64 m,i ...

  5. Spring 注解@Transactional

    Spring事务的传播行为 在service类前加上@Transactional,声明这个service所有方法需要事务管理.每一个业务方法开始时都会打开一个事务. Spring默认情况下会对运行期例 ...

  6. MDM平台学习笔记

    最近和将来一段时间都会花很多时间在主数据管理平台的学习和开发上,从现在开始打算记录此过程中的知识点和学习心得,加油! 1.IBM全新的产品文档网站IBM Knowledge Center,软件硬件产品 ...

  7. 基于Linux的oracle数据库管理 part2( 数据库 准备,安装,创建 )

    主要内容 1. 准备 2. 安装 与 删除 软件 3. 创建数据库 4. 配置 SQL*PLUS 环境 准备 1. 软件包, rpm –qa , rpm –ivh *.rpm 2. 检查磁盘空间 3. ...

  8. 内存分配(c/c++)

    C++中内存分配          内存分成5个区,他们分别是堆.栈.自由存储区.全局/静态存储区和常量存储区. 1,栈,就是那些由编译器在需要的时候分配,在不需要的时候自动清除的变量的存储区.里面的 ...

  9. C语言中的三值合一

    在学习C语言中我们会发现这样一种情况: #include<stdio.h> Int main() { Int ar[10]; printf(“%p\n”,ar); printf(“%p\n ...

  10. github每次推送都要输入用户名和密码

    /***************************************************************************** * github每次推送都要输入用户名和密 ...