[抄题]:

Input: "42"
Output: 42

Example 2:

Input: "   -42"
Output: -42
Explanation: The first non-whitespace character is '-', which is the minus sign.
  Then take as many numerical digits as possible, which gets 42.

Example 3:

Input: "4193 with words"
Output: 4193
Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.

Example 4:

Input: "words and 987"
Output: 0
Explanation: The first non-whitespace character is 'w', which is not a numerical
  digit or a +/- sign. Therefore no valid conversion could be performed.

Example 5:

Input: "-91283472332"
Output: -2147483648
Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
  Thefore INT_MIN (−231) is returned.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

根本不知道应该怎么处理越界啊:

先设置一个bound变量,-2147483648/10。当前num > bound || num == bond & digit > 7都不行

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

写整齐点,要考虑到的问题:空格(用trim)、符号(用标记变量)、越界

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 注意要把string转成字符才能操作
  2. 字符不是统一处理的 在符号处理和越界处理之后,都要再分别进行i++

[二刷]:

  1. 整数的范围是 ‘0’  <= c <= '9',必须有等号

[三刷]:

  1. num的进位方式是 num = num * 10 + digit digit是最后一位数,不用新相乘

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

digit是最后一位数,不用新相乘

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

class Solution {
public int myAtoi(String str) {
//handle space
str = str.trim();
int i = 0;
char[] c = str.toCharArray(); //signs
int sign = 1;
if (i < c.length && (c[i] == '+' || c[i] == '-')) {
if (c[i] == '-') sign = -1;
i++;
} //out of bound in two ways
int bound = Integer.MAX_VALUE / 10;
int num = 0;
while (i < c.length && (c[i] >= '0' && c[i] <= '9')){
int digit = c[i] - '0'; //out of bound
if (num > bound || (num == bound && digit > 7)) {
//depend on sign
return (sign == 1) ? Integer.MAX_VALUE : Integer.MIN_VALUE;
}
num = digit + num * 10;
i++;
} return num * sign;
}
}

8. String to Integer (atoi) 字符串转成整数的更多相关文章

  1. [Leetcode] String to integer atoi 字符串转换成整数

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

  2. 【LeetCode每天一题】String to Integer (atoi)(字符串转换成数字)

    Implement atoi which converts a string to an integer.The function first discards as many whitespace ...

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

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

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

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

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

    这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...

  6. Leetcode 8 String to Integer (atoi) 字符串处理

    题意:将字符串转化成数字. 前置有空格,同时有正负号,数字有可能会溢出,这里用long long解决(leetcode用的是g++编译器),这题还是很有难度的. class Solution { pu ...

  7. [leetcode]8. String to Integer (atoi)字符串转整数

    Implement atoi which converts a string to an integer. The function first discards as many whitespace ...

  8. [LeetCode] String to Integer (atoi) 字符串转为整数

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

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

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

随机推荐

  1. PythonStudy——列表类型 List type

    # 1.定义 ls = [3, 1, 2] # 语法糖 | 笑笑语法 print(ls) ls = list([3, 1, 2]) # 本质 print(ls) # 嵌套 ls = [3, 1, [3 ...

  2. esxi5 的tart命令使用注意点

    esxi5.0 注意tar命令参数使用和centos6稍微有点不一样,注意下 注意需要把-f参数单独分离出来,紧接着文件.   而不能和cz命令一起用 ~ # touch abc.txt ~ # ec ...

  3. 我发起了一个 .Net 开源 跨平台 GUI (界面开发框架)项目 HtmlCore

    大家好 , 我发起了一个 .Net 开源 跨平台 GUI (界面开发框架)项目 , 名字叫 HtmlCore  . 项目的一个主要目标是可以用 .Net 在 移动设备 上 开发 GUI 程序 (界面程 ...

  4. 删除iis日志(deliislogs.vbs)

    'path 目录 'ext 文件扩展名'expiredDays 保留多少天以内的文件Sub LogCleaner(path,ext,expiredDays) On Error Resume Next ...

  5. LeetCode——727.Minimum Window Subsequence

    一.题目链接:https://leetcode.com/problems/minimum-window-substring/ 二.题目大意: 给定两个字符串S和T,要求从S中找出包含T中所有字母的最短 ...

  6. 廖雪峰Java6 IO编程-3Reader和Writer-1Reader

    1.java.io.Reader和java.io.InputStream的区别 InputStream Reader 字节流,以byte为单位 字符流,以char为单位 读取字节(-1,0-255): ...

  7. python之路——12

    王二学习python的笔记以及记录,如有雷同,那也没事,欢迎交流,wx:wyb199594 复习 1.装饰器 开发原则:开放封闭原则 作用:不改变原函数的调用方式,为函数前后扩展功能 本质:闭包函数 ...

  8. 阿里Java开发手册

    1.1 命名风格 (1)常量命名全部大写,单词间用下划线隔开. (2)抽象类命名以Abstract或Base开头:异常类命名以Exception结尾:测试类命名以它要测试的类名开始,以Test结尾. ...

  9. django 补充和中间件

    配置 from django.conf import settings form组件 from django.forms import Formfrom django.forms import fie ...

  10. H5的本地存储技术及其与Cookie的比较

    第一部分: H5的本地存储技术 HTML5 提供了两种在客户端存储数据的新方法.先看下面的例子: 例1:var mySelection = {name:"car", amount: ...