首页 技术 正文
技术 2022年11月8日
0 收藏 751 点赞 2,037 浏览 2370 个字

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)字符串转整数

下一篇: OS history
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,957
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,481
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,327
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,110
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,742
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,776