[leetcode]8. String to Integer (atoi)字符串转整数
Implement atoi which converts a string to an integer.
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned.
Note:
- Only the space character
' 'is considered as whitespace character. - Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 2^31 − 1]. If the numerical value is out of the range of representable values, INT_MAX (2^31 − 1) or INT_MIN (−231) is returned.
Example 1:
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
代码
class Solution {
public int myAtoi(String str) {
str = str.trim(); // handle str = " "的情况, 此时str.length() = 4, 不会走第一个if语句,从而会造成后续指针的outofbound
if (str == null || str.length() == 0)
return 0;
char c = str.charAt(0);
int sign = 1, start = 0, len = str.length();
long sum = 0;
// take care of sign
if (c == '+') {
sign = 1;
start++;
} else if (c == '-') {
sign = -1;
start++;
}
for (int i = start; i < len; i++) {
// take care of non numerical digit, like 'w'
if (!Character.isDigit(str.charAt(i))){
return (int) sum * sign;
}
// convert each char to each digit
sum = sum * 10 + str.charAt(i) - '0';
// Input: "-91283472332" Output:-2147483648
if (sign == 1 && sum > Integer.MAX_VALUE){
return Integer.MAX_VALUE;
}
//思考为何不能写成 if(sign == -1 && sum > Integer.MAX_VALUE)
if (sign == -1 && (-1) * sum < Integer.MIN_VALUE){
return Integer.MIN_VALUE;
}
}
return (int) sum * sign;
}
}
注意: line11-14 显得很多余,因为通常正整数前不会专门写 ‘+’ 号。 但确实有这样的test case(如下图), 所以写上这个条件,会更加严谨。

[leetcode]8. String to Integer (atoi)字符串转整数的更多相关文章
- [LeetCode] 8. String to Integer (atoi) 字符串转为整数
Implement atoi which converts a string to an integer. The function first discards as many whitespace ...
- 【LeetCode】String to Integer (atoi)(字符串转换整数 (atoi))
这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...
- [LeetCode] String to Integer (atoi) 字符串转为整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 【LeetCode】8. String to Integer (atoi) 字符串转换整数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...
- Leetcode8.String to Integer (atoi)字符串转整数(atoi)
实现 atoi,将字符串转为整数. 该函数首先根据需要丢弃任意多的空格字符,直到找到第一个非空格字符为止.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字 ...
- 【LeetCode】8. String to Integer (atoi) 字符串转整数
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- Leetcode 8 String to Integer (atoi) 字符串处理
题意:将字符串转化成数字. 前置有空格,同时有正负号,数字有可能会溢出,这里用long long解决(leetcode用的是g++编译器),这题还是很有难度的. class Solution { pu ...
- LeetCode OJ String to Integer (atoi) 字符串转数字
#include <iostream> #include <assert.h> using namespace std; int ato(const char *str) { ...
- 008 String to Integer (atoi) 字符串转换为整数
详见:https://leetcode.com/problems/string-to-integer-atoi/description/ 实现语言:Java class Solution { publ ...
随机推荐
- erlang遍历目录
{ok, Cwd} = file:get_cwd(). Filelist = filelib:fold_files( Cwd, ".*", true, fun(File, Acc) ...
- instant client 的配置
instant client 的配置 oracle server developer自带了客户端 解压目录:D:\Toolkit\instantclient_11_2 设置环境变量 Ø 在Path变 ...
- uniDAC的安装和使用
1.解压后把UniDAC文件夹 2.在UniDAC\Source\Delphi7文件夹中找到Make.bat文件,鼠标右键“编辑”确认DELPHI7的安装路径是否正确(建议:设置成绝对路径了,防止因为 ...
- PO & SO Integration By IDOC in CNABB
PO & SO Integration By IDOC in CNABB 话说博主来ABB一个多月时间了,虽然对ABB系统内的流程和配置不是很了解,但对ABB系统内使用的PO和SO通过idoc ...
- DELL服务器r710配置RAID
DELL服务器r710配置RAID: 1.开机后等待DELL图标出现,连续按ctrl+r,进入RAID配置界面: 2.进入RAID配置界面,如果是下面的这个界面,选择Disk group 0这项需要按 ...
- 多端统一框架尝试--Taro
参考资料 Taro官网Taro GitHubTaro资源汇总Taro-UI 我的demo代码 github地址 Taro介绍和尝试心得 Taro是基于React语法规范开发的多端统一的框架,一套代码可 ...
- 关于offsetParent
不知道有多少人知道JavaScript中有offsetParent这么个属性. 关于offsetParent,我最开始见到他,是在<JavaScript高级程序设计(第3版)>第321页, ...
- python程序正式开始
第几个hello world 程序了,为曾经没有毅力的自己默哀下.今天的课程语言的分类,三大类:机器语言,汇编语言,高级语言. 其中最让我痛恨的就是汇编语言,我们大学没事开什么这课程,大学混日子的喔不 ...
- cookies的常见方式
cookie有如下特点 保存在客户端,一般由浏览器负责存储在本地. 通常是加密存储的,不过由于存储在本地,很难保证数据不被非法访问,并不怎么安全,所以cookies中不宜保存敏感信息,如密码等. 哪些 ...
- Django模板语言相关内容 Djan
Django模板语言相关内容 Django模板系统 官方文档 常用语法 只需要记两种特殊符号: {{ }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 变量 {{ 变量名 }} ...