2015-01-19 21:22:51

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 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.

1. public class Solution {
2.     public int atoi(String str) {
3.         if(str.length() == 0){
4.             return 0;
5.         }
6.         int sum = 0;
7.         char[] myarr = str.toCharArray();
8.         int i = 0;
9.         int symbol = 1;
10.         while(myarr[i] == ' '){
11.             i++;
12.         }
13.         int j = i;
14.         if(myarr[j] == '-'){
15.             symbol = -1;
16.             j++;
17.         }
18.         else if(myarr[j] == '+'){
19.             j++;
20.         }
21.         //System.out.println(j);
22.         for(int n = j;n < myarr.length; n++){
23.             //System.out.println(Character.getNumericValue(myarr[n]));
24.             if(Character.getNumericValue(myarr[n]) >= 0 && Character.getNumericValue(myarr[n]) <= 9){
25.                 if(sum == Integer.MAX_VALUE || sum == Integer.MIN_VALUE){
26.                     continue;
27.                 }
28.                 else if(Math.abs(sum) > Integer.MAX_VALUE/10){
29.                     if(symbol == 1){
30.                         sum = Integer.MAX_VALUE;
31.                     }
32.                     else{
33.                         sum = Integer.MIN_VALUE;
34.                     }
35.                 }
36.                 else if(Math.abs(sum) == Integer.MAX_VALUE/10){
37.                     if(symbol == 1){
38.                         if(Character.getNumericValue(myarr[n]) > Integer.MAX_VALUE%10){
39.                             sum = Integer.MAX_VALUE;
40.                         }
41.                         else{
42.                             sum = sum*10 + Character.getNumericValue(myarr[n]);
43.                         }
44.                     }
45.                     else{
46.                         if(Character.getNumericValue(myarr[n]) > Math.abs(Integer.MIN_VALUE%10)){
47.                             sum = Integer.MIN_VALUE;
48.                         }
49.                         else{
50.                             sum = -(Math.abs(sum)*10 + Character.getNumericValue(myarr[n]));
51.                         }
52.                     }
53.                 }
54.                 else{
55.                     if(symbol == 1){
56.                         sum = sum*10 + Character.getNumericValue(myarr[n]);
57.                     }
58.                     else{
59.                         sum = -(Math.abs(sum)*10 + Character.getNumericValue(myarr[n]));
60.                     }
61.                 }
62.             }
63.             else{
64.                 return sum;
65.             }
66.         }
67.         return sum;
68.     }
69. }
PS：该题在LeetCode中分级为easy但是acceptance只有13.7%，排在179题中倒数第5，由此可见思维的全面性的重要性