[LeetCode]8. String to Integer (atoi)

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.

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.

public class Solution {
public int myAtoi(String str) {
long sum = 0;
if(!str.equals("")){
char[] mm = str.toCharArray();
int[] ii = new int[mm.length+1];
boolean begin = false;
int b = 0;
for(int a=0;a<mm.length;a++){
if(mm[a]==' '&&!begin)
continue;
if(!begin&&(mm[a]=='+'||mm[a]=='-'||(mm[a]>='0'&&mm[a]<='9'))){
ii[0] = mm[a]=='-'?-1:1;
if(a<mm.length-1&&(mm[a]=='-'||mm[a]=='+'))
a++;
begin = true;
}

if(mm[a]>='0'&&mm[a]<='9'){
ii[++b] = mm[a]-'0';
}else{
break;
}
}
for(int x=0;b>0;b--){
sum += ii[b]*(Math.pow(10, x));
x++;
}
sum *= ii[0];
if(sum>2147483647)
sum = Integer.MAX_VALUE;
if(sum<-2147483648)
sum = Integer.MIN_VALUE;
}
return (int)sum;
}
}

4ms:

            if (str == null || str.length() < 1)
return 0;

// trim white spaces
str = str.trim();

char flag = '+';

// check negative or positive
int i = 0;
if (str.charAt(0) == '-') {
flag = '-';
i++;
} else if (str.charAt(0) == '+') {
i++;
}
// use double to store result
double result = 0;

// calculate value
while (str.length() > i && str.charAt(i) >= '0' && str.charAt(i) <= '9') {
result = result * 10 + (str.charAt(i) - '0');
i++;
}

if (flag == '-')
result = -result;

// handle max and min
if (result > Integer.MAX_VALUE)
return Integer.MAX_VALUE;

if (result < Integer.MIN_VALUE)
return Integer.MIN_VALUE;

return (int) result;

Leetcode-8-String to Integer(atoi) C++

2016-06-27 14:24:44

LeetCode题解--8. String to Integer (atoi)

2016-04-02 23:10:39

[JAVA]LeetCode8 String to Integer (atoi)

2015-04-15 16:09:20

【LeetCode】String to Integer (atoi) 解题报告

2014-10-27 16:19:38

LeetCode 8 String to Integer (atoi) (C,C++,Java,Python)

2015-05-07 10:38:53

【LeetCode】LeetCode——第8题：String to Integer (atoi)

2016-04-19 10:26:12

LeetCode（8）String to Integer (atoi)

2015-04-27 16:57:18

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

2016-03-04 15:02:52

leetcode 8: String to Integer (myAtoi，C库函数atoi模拟) (剑指offer 面试题49) 解题报告

2016-05-03 10:13:27

8. String to Integer (atoi) [easy] (Python)

2016-07-28 11:47:13

不良信息举报

[LeetCode]8. String to Integer (atoi)