-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalid_anagram.java
55 lines (40 loc) · 1.31 KB
/
valid_anagram.java
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
import java.io.*;
class Solution {
public int myAtoi(String str) {
final int len = str.length();
if(len == 0){
return 0;
}
int index = 0;
while(index < len && str.charAt(index) == ' '){
index++;
}
if(index == len){
return 0;
}
char ch;
boolean isNegative = (ch = str.charAt(index)) == '-';
if(isNegative || ch == '+'){
++index;
}
final int maxLimit = Integer.MAX_VALUE / 10;
int result = 0;
while(index < len && isDigit(ch = str.charAt(index))){
int digit = ch - '0';
if(result > maxLimit || (result == maxLimit && digit > 7)){
return isNegative ? Integer.MIN_VALUE : Integer.MAX_VALUE;
}
result = (result * 10) + digit;
++index;
}
return isNegative ? -result : result;
}
public static void main(String[] args) {
System.out.println("hello world");
Solution sc = new Solution();
System.out.println(sc.myAtoi("Hello12435"));
}
private boolean isDigit(char ch){
return ch >= '0' && ch <= '9';
}
}