/*
8. String to Integer (atoi)
https://leetcode.com/problems/string-to-integer-atoi/
Total Accepted: 93777 Total Submissions: 698810 Difficulty: Easy
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 specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.
spoilers alert... click to show requirements for atoi.
Requirements for atoi:
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. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
Hide Company Tags Amazon Microsoft Bloomberg Uber
Hide Tags Math String
Hide Similar Problems (E) Reverse Integer (H) Valid Number
Have you met this question in a real interview? Yes No
Discuss
Date : 2016.03.22
*/
import java.util.*;
public class MyAtoi {
public static int myAtoi(String s) {
int sign = 1, i = 0, num = 0;
int n = s.length();
int maxDiv10 = Integer.MAX_VALUE/10;
if (s == null || n == 0) return 0;
// deal with multi whitespace
while ( i < n && Character.isWhitespace(s.charAt(i))) i++;
if (i < n && s.charAt(i) == '+') {
sign = 1;
i++;
}
if (i < n && s.charAt(i) == '-') {
sign = -1;
i++;
}
while (i < n && Character.isDigit(s.charAt(i))) {
int digit = Character.getNumericValue(s.charAt(i));
if (num > maxDiv10 ||
(num < maxDiv10 && digit > 8))
return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
num = num * 10 + digit;
i++;
}
return sign * num;
}
public static int myAtoi_overflow(String str) {
if(str == null && str.length() ==0 ) return 0;
StringBuilder sb = new StringBuilder();
for (char c : str.toCharArray()) {
if( !Character.isWhitespace(c)) {
sb.append(c);
}
}
if ( !sb.toString().equals("")) {
int res = Integer.parseInt(sb.toString());
return res ;
}
return 0 ;
}
public static void main(String[] args) {
String[] testcases = {"", "-2321", "30000001","-2198563", "2147484647", "-2147484648", "2147484649"};
int i=0;
for(String str : testcases) {
System.out.println( i + " : " + myAtoi(str));
i++;
}
}
}
8. String to Integer (atoi)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 实现atoi将字符串转换为整数。提示:仔细考虑所有可能的输入案例。如果你想挑战,请不要在下面看到,问问自己可能输入...
- https://leetcode.com/problems/string-to-integer-atoi/disc...
- Implement atoi to convert a string to an integer. Hint: C...
- Implement atoi to convert a string to an integer. Hint: C...
- Implementatoito convert a string to an integer.Hint:Caref...