[leetcode]_String to Integer (atoi)

非常考虑思维全面性的一道题,考验是否能够考虑本问题的方方面面。

题目:将一个string转换为int。实现函数atoi()的功能。

先应该明确atoi()有哪些特殊功能:(正常的正负数情况我就不列了)




















input output
”+1“ 1
”   +   1“  0(error了)
”       1“ 1(前头只有空格是合法的)
”12b45“ 12(取前面的数字)
溢出 : ”2147483648“(负数情况同) 2147483647(MAX_VALUE)

类似我对atoi()的功能不熟的人来说,这道题就是不停WA的血泪史。每次要修正一次错误答案。

最终AC:


public int atoi(String str) {
int len = str.length();
long num = 0;
//用long型存储,以处理溢出的情况
int ifNegative = 1;
boolean numStatus = false;
for(int i = 0 ; i < len ; i++){
char ch = str.charAt(i);
if(numStatus && (ch < ‘0‘ || ch > ‘9‘)) break;
else if(numStatus && ch >= ‘0‘ && ch <= ‘9‘){
num = num * 10 + (ch - ‘0‘);
}else if(!numStatus && ch != ‘-‘ && ch != ‘+‘ && (ch < ‘0‘ || ch > ‘9‘)){
num = 0;
break;
}else if(!numStatus && ch == ‘-‘){
numStatus = true;
ifNegative = -1;
}else if(!numStatus && ch == ‘+‘){
numStatus = true;
}else if(!numStatus && ch >= ‘0‘ && ch <= ‘9‘){
numStatus = true;
num = num * 10 + (ch - ‘0‘);
}

}
num *= ifNegative;

int result = 0;
if(num > Integer.MAX_VALUE) result = Integer.MAX_VALUE;
else if(num < Integer.MIN_VALUE) result = Integer.MIN_VALUE;
else result = (int) num;

return result;

}

[leetcode]_String to Integer (atoi),布布扣,bubuko.com

时间: 2024-10-07 05:29:31

[leetcode]_String to Integer (atoi)的相关文章

leetcode——String to Integer (atoi) 字符串转换为整型数(AC)

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

LeetCode——String to Integer (atoi)

Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be spe

[leetcode] [leetcode] String to Integer (atoi)

Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be spe

[LeetCode] String to Integer (atoi) [7]

题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be

(atoi)Leetcode第八题_String to Integer (atoi)

String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended f

leetcode - String to Integer (atoi) 字符串转整数

Implement atoi to convert a string to an integer. Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. 题目:实现字符串转整数 注意事项:考虑好各种可能的输入(坑); public class Solution

[LeetCode] String to Integer (atoi)

Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be spe

[LeetCode]-007-String to Integer (atoi)

网址:https://leetcode.com/problems/string-to-integer-atoi/ 题意: 字符串转int数 分析: 经典题,主要需要注意输入中,允许先有空格,再来内容. 内容可能还不是整齐和规则的... 解法: 1.遍历空格 2.判断正负号 3.读数字 4.可能存在结尾号 代码: https://github.com/LiLane/leetcode/blob/master/java/008-StringtoInteger(atoi)-201504292346.ja

[LeetCode] String to Integer (atoi) 字符串

Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be spe