-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringToInteger.cs
More file actions
37 lines (35 loc) · 1.25 KB
/
Copy pathStringToInteger.cs
File metadata and controls
37 lines (35 loc) · 1.25 KB
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
public class Solution {
public int MyAtoi(string str) {
str = str.Trim();
if (str == null || str == string.Empty) return 0;
// first non-white char is not number or '+' or '-'
if (!Char.IsDigit(str[0]) && (str[0] != '-') && (str[0] != '+')) {
return 0;
}
bool negative = false;
int answer = 0;
int size = str.Length;
if (str[0] == '-') {
negative = true;
str = str.Substring(1);
size--;
} else if (str[0] == '+') {
str = str.Substring(1);
size--;
}
// string starts with number
for (int i = 0; i < size; i++) {
if (!Char.IsDigit(str[0])) break; // stop when encounter non-digit character
int pop = (int)Char.GetNumericValue(str[0]) * (negative ? -1 : 1);
if (answer < Int32.MinValue/10 || (answer == Int32.MinValue/10 && pop < -8)) {
return Int32.MinValue;
}
if (answer > Int32.MaxValue/10 || (answer == Int32.MaxValue/10 && pop > 7)) {
return Int32.MaxValue;
}
answer = answer * 10 + pop;
str = str.Substring(1);
}
return answer;
}
}