String to Integer (atoi)
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.

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.

Solution

1. 用String s = str.trim();裁掉前面后面的空格。

2. 记录起始的+,-符号。

3. 用Long来转数字,这样的话就算越界也没什么了。

 1 public class Solution {
 2     public int atoi(String str) {
 3         if (str == null) {
 4             return 0;
 5         }
 6         
 7         // remove the spaces in the begining and the end.
 8         String s = str.trim();
 9         
10         boolean minus = false;
11         long num = 0;
12         for (int i = 0; i < s.length(); i++) {
13             /*
14              takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them              as a numerical value.
15             */
16             if (i == 0 && s.charAt(i) == '+') {
17                 continue;
18             } else if (i == 0 && s.charAt(i) == '-'){
19                 // get the 
20                 minus = true;
21                 continue;
22             }
23             
24             int c = s.charAt(i) - '0';
25             if (c > 9 || c < 0) {
26                 // invalid character.
27                 break;
28             }
29             
30             num = num * 10 + c;
31         }
32         
33         // If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is           // returned.
34         if (minus) {
35             num = -num;
36             num = Math.max(num, Integer.MIN_VALUE);
37         } else {
38             num = Math.min(num, Integer.MAX_VALUE);
39         }
40         
41         return (int)num;
42     }
43 }
View Code

相关文章:

  • 2021-09-14
  • 2021-05-21
  • 2021-06-07
  • 2021-09-17
  • 2022-12-23
猜你喜欢
  • 2021-08-15
  • 2022-12-23
  • 2021-06-21
  • 2022-01-20
  • 2021-10-27
  • 2021-12-08
  • 2022-01-12
相关资源
相似解决方案