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.
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.
2.數字前出現了不必要或多於的字符導致數字認證錯誤,返回0 如s=“ b1234” , s=“ +-1121”
3.數字中出現了不必要的字符,返回字符前的數字 如s=“ 12a12” 返回12
4.溢出情況,超過了範圍(-2147483648~2147483647) 若超過了負數的 輸出-2147483648 超過了正數的輸出2147483647
在科普一個知識點,倘若某個數超過了2147483647則會變爲負數,反過來一樣
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
public class Solution { public int myAtoi(String str) { //使用正則表達式去掉字符串中空格字符 str = str.trim(); //flag爲字符串所包含的符號,-1代表負數,1代表正數 int flag = 1 ; //用來判斷是否有+-或者-+出現 int preFlagNum = 0 ; int curFlagNum = 0 ; //result代表字符串中的整型數 double result = 0 ; //表示除去符號位剩下的數字位的位置 int begin = 0 ; //如果str爲空則無整數存在,返回0 if (str.length()> 0 ){ //判斷正負數 if (!Character.isDigit(str.charAt( 0 ))){ for ( int i= 0 ;i<str.length();i++){ if (str.charAt(i)== '-' ){ flag = -flag; //判斷是否有-+的情況出現,若有,則返回0 preFlagNum = curFlagNum; curFlagNum--; if (i!= 0 &&curFlagNum<= 0 &&curFlagNum<preFlagNum){ return 0 ; } } else if (str.charAt(i)== '+' ){ preFlagNum = curFlagNum; curFlagNum++; if (i!= 0 &&curFlagNum>= 0 &&curFlagNum>preFlagNum){ return 0 ; } } else { break ; } begin++; } } //計算數字部分的值 while (str.length()>begin&&str.charAt(begin)>= '0' &&str.charAt(begin)<= '9' ){ result = result * 10 + (str.charAt(begin) - '0' ); begin++; } //負數加負號 if (flag==- 1 ){ result = -result; } //判斷是否超出最大整數值 if (result > Integer.MAX_VALUE) return Integer.MAX_VALUE; //判斷是否超出最小整數值 if (result < Integer.MIN_VALUE) return Integer.MIN_VALUE; //轉換爲整型 return ( int ) result; } else { return 0 ; } } } |