详细见:leetcode.com/problems/string-to-integer-atoi/
Java Solution: github
package leetcode;
public class P008_StringToInteger {
public static void main(String[] args) {
// System.out.println();
System.out.println(new Solution1().myAtoi(" -1123u3761867"));
}
//重点还是在判断溢出上面。
/*
* atoi多奇葩
* 4 ms
* 35.83%
*/
static class Solution1 {
public int myAtoi(String str) {
int len = 0;
if (str == null)
return 0;
str = str.trim();
if ((len = str.length()) == 0)
return 0;
int offset = 0;
boolean isNegative = false;
switch (str.charAt(0)) {
case '-':
offset = 1;
isNegative = true;
break;
case '+':
offset = 1;
break;
default:
break;
}
int ans = 0;
for (int i = offset ; i != len; i ++) {
char c = str.charAt(i);
if (c >= '0' && c <= '9') {
int add = c - '0';
if (ans > 214748364 || (ans == 214748364 && add > 7 + (isNegative ? 1 : 0)))
return isNegative ? Integer.MIN_VALUE : Integer.MAX_VALUE;
if (ans == 214748364 && add == 8 && isNegative && i == len - 1)
return Integer.MIN_VALUE;
ans = ans * 10 + c - '0';
} else
return isNegative ? -ans : +ans;
}
return isNegative ? -ans : +ans;
}
}
/*
* 81.44%
* 3ms
* 最短。
*/
static class Solution2 {
public int myAtoi(String str) {
int len = 0;
if (str == null)
return 0;
if ((len = str.length()) == 0)
return 0;
int offset = 0;
while (str.charAt(offset) == ' ') {
offset ++;
}
if (offset >= len)
return 0;
boolean isNegative = false;
switch (str.charAt(offset)) {
case '-':
offset ++;
isNegative = true;
break;
case '+':
offset ++;
break;
default:
break;
}
int ans = 0;
for (int i = offset ; i != len; i ++) {
char c = str.charAt(i);
if (c >= '0' && c <= '9') {
int add = c - '0';
if (ans > 214748364 || (ans == 214748364 && add > 7 + (isNegative ? 1 : 0)))
return isNegative ? Integer.MIN_VALUE : Integer.MAX_VALUE;
if (ans == 214748364 && add == 8 && isNegative && i == len - 1)
return Integer.MIN_VALUE;
ans = ans * 10 + c - '0';
} else
return isNegative ? -ans : +ans;
}
return isNegative ? -ans : +ans;
}
}
}
/*
url: leetcode.com/problems/string-to-integer-atoi/
25ms 12.81%
*/
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
int myAtoi(char* str) {
int index = 0;
int a = 0;
int s = 1;
char c = '\0';
while (*(str + index) == ' ')
index ++;
if ('+' == *(str + index)) {
index ++;
} else if ('-' == *(str + index)) {
s = -1;
index ++;
}
while (1) {
c = *(str + (index ++));
if (c < '0' || c > '9')
break;
if (a > 0 && a < 10 && a * s < 0)
a = -a;
if (a > INT_MAX / 10 || (a == INT_MAX / 10 && c - '0' > INT_MAX % 10))
return INT_MAX;
else if (a < INT_MIN / 10 || (a == INT_MIN / 10 && c - '0' > -(INT_MIN % 10)))
return INT_MIN;
a = a * 10 + (s * (c - '0'));
}
return a;
}
int main() {
//sample of atoi
int n;
char *str = "2147483648";
long l = INT_MIN;
int v = (int)l;
printf("int_max is %d\t%d\r\n", v, INT_MIN);
printf("my answer is %d\r\n", myAtoi(str));
n = atoi(str);
printf("string = %s integer = %d\n", str, n);
}
Python Solution: github
#coding=utf-8
'''
url: leetcode.com/problems/string-to-integer-atoi/
@author: zxwtry
@email: zxwtry@qq.com
@date: 2017年3月27日
@details: Solution: AC 72ms 63.62%
'''
class Solution(object):
def myAtoiHelper(self, s):
s_len = len(s)
int_val = 0
ord_i, ord_0, ord_9 = 0,ord('0'),ord('9')
for i in range(s_len):
ord_i = ord(s[i])
if ord_i >= ord_0 and ord_i <= ord_9:
int_val = int_val * 10 + ord_i - ord_0
else:
return int_val
return int_val
def myAtoi(self, s):
"""
:type s: s
:rtype: int
"""
if s == None: return 0
s = s.strip()
s_len = len(s)
if s_len == 0: return 0
val = 0
if s[0] == '+': val = self.myAtoiHelper(s[1:])
elif s[0] == '-': val = -self.myAtoiHelper(s[1:])
else: val = self.myAtoiHelper(s)
if val > 2147483647:
val = 2147483647
elif val < -2147483648:
val = -2147483648
return val
if __name__ == "__main__":
s = " 214748a3648"
sol = Solution()
print(sol.myAtoi(s))