Leetcode -- Day 51 & Day 55

String to Integer (atoi) / valid number

Question 1

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.

 1 public int myAtoi(String str) {
 2         if (str == null || str.length() < 1){
 3             return 0;
 4         }
 5         str = str.trim();
 6         boolean isNeg = false;
 7         int i = 0;
 8         
 9         if (str.charAt(0) == '-'){
10             isNeg = true;
11             str = str.substring(1);
12         }
13         else if (str.charAt(0) == '+'){
14             str = str.substring(1);
15         }
16         
17         if (!Character.isDigit(str.charAt(0))){
18             return 0;
19         }
20         
21         double result = 0;
22         for (char c : str.toCharArray()){
23             if (Character.isDigit(c))
24                 result = result * 10 + (c-'0');
25             else 
26                 break;
27         }
28         if (isNeg){
29             result = - result;
30             return result < Integer.MIN_VALUE ? Integer.MIN_VALUE : (int) result;
31         }
32         else{
33             return result > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) result;
34         }
35     }

Question 2

Valid Number

Validate if a given string is numeric.

Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true

Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.

For this problem, wee need to set eIndex and dotIndex to record the index of char e and . , which is very important for the special valid number cases.

 1     public boolean isNumber(String s) {
 2         s = s.trim();
 3         int eIndex = -1;
 4         int dotIndex = -1;
 5         
 6         if (s.length()==0)
 7             return false;
 8         
 9         int i = 0;
10         
11         if (s.charAt(i) == '+' || s.charAt(i) == '-')
12             s = s.substring(1);
13             
14         for (i = 0; i < s.length(); i ++){
15             if (eIndex == -1 && s.charAt(i) == 'e'){
16                 eIndex = i;
17                 if (i + 1 < s.length() && (s.charAt(i+1) == '-' || s.charAt(i+1) == '+'))
18                     i ++;
19             }
20             else if (dotIndex == -1 && s.charAt(i) == '.')
21                 dotIndex = i;
22             else{
23                 if (Character.isDigit(s.charAt(i)))
24                     continue;
25                 else
26                     return false;
27             }
28         }
29         
30         String startString, midString, endString;
31         if (eIndex == -1 && dotIndex == -1){
32             startString = s;
33             if (startString.length() < 1)
34                 return false;
35         }
36         else if (eIndex == -1 && dotIndex != -1){
37             startString = s.substring(0, dotIndex);
38             endString = s.substring(dotIndex+1);
39             if (startString.length()<1 && endString.length() < 1)
40                 return false;
41         }
42         else if (dotIndex == -1 && eIndex != -1){
43             startString = s.substring(0, eIndex);
44             if (startString.length()<1)
45                 return false;
46             if (eIndex+1 < s.length() && (s.charAt(eIndex+1) == '+' ||s.charAt(eIndex+1) == '-'))
47                 endString = s.substring(eIndex + 2);
48             else
49                 endString = s.substring(eIndex + 1);
50             if (endString.length()<1)
51                 return false;
52         }
53         else{
54             if (dotIndex > eIndex)  return false;
55             else{
56                 startString = s.substring(0, dotIndex);
57                 midString = s.substring(dotIndex+1, eIndex);
58                 if (startString.length()<1 && midString.length()<1)
59                     return false;
60                 if (eIndex+1 < s.length() && (s.charAt(eIndex+1) == '+' ||s.charAt(eIndex+1) == '-'))
61                     endString = s.substring(eIndex + 2);
62                 else
63                     endString = s.substring(eIndex + 1);
64                 if (endString.length()<1 )
65                     return false;
66             }
67         }
68         return true;
69     }
原文地址:https://www.cnblogs.com/timoBlog/p/4748144.html