-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8 String to Integer (atoi).dart
54 lines (41 loc) · 1.34 KB
/
8 String to Integer (atoi).dart
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
class Solution {
static const MAX = 2147483647;
static const MIN = -2147483648;
int myAtoi(String s) {
//Enumeration variable.
int idx = 0;
//Variable to store result.
int result = 0;
//Boolean to decide sign.
bool isNeg = false;
//Untill there are white spaces, keep incrementing.
while (idx < s.length && s[idx] == ' ') idx++;
//If we encounter sign, change boolean value and increment.
if (idx < s.length && (s[idx] == '+' || s[idx] == '-')) {
isNeg = s[idx] == '-';
idx++;
}
//Untill we keep encountering digits, keep adding it to result.
//(s.codeUnitAt(idx) ^ 0x30) <= 9, this will return true if we have digut.
while (idx < s.length && (s.codeUnitAt(idx) ^ 0x30) <= 9) {
//Converting characters to numbers.
result = result * 10 + int.parse(s[idx]);
//If value exceeds MaxValue, break the loop.
if (result > MAX) break;
//Keep incrementing index.
idx++;
}
//If value needs to be negative, convert it.
result = isNeg ? -result : result;
//If values are max, return MaxValue.
if (result > MAX) return MAX;
//If values are min, return MinValue.
if (result < MIN) return MIN;
//Return current result.
return result;
}
}
void main() {
Solution sol = Solution();
print(sol.myAtoi("-4193 with words"));
}