-
Notifications
You must be signed in to change notification settings - Fork 0
/
string-to-integer-atoi.js
103 lines (83 loc) · 1.9 KB
/
string-to-integer-atoi.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// 方法一:一般解法
/**
* 字符串转换整数 (atoi)
* @param {string} s
* @return {number}
*/
var myAtoi = function (s) {
let flag = 1; // 1:正数 1-:负数
s = s.trimStart();
if (s[0] === "-") {
flag = -1;
s = s.slice(1);
} else if (s[0] === "+") {
s = s.slice(1);
}
let arr = [];
let count = 0;
for (let i = 0; i < s.length; i++) {
let curr = s[i];
if (+curr === +curr && count === i && curr !== " ") {
arr.push(curr);
count++;
}
}
let result = arr.length > 0 ? Number(arr.join("")) : 0;
result *= flag;
const min = -Math.pow(2, 31);
const max = Math.pow(2, 31) - 1;
if (result < min) {
result = min;
} else if (result > max) result = max;
return result;
};
// 方法二:有限自动机 (DFA)
/**
* 字符串转换整数 (atoi)
* @param {string} s
* @return {number}
*/
var myAtoi = function (s) {
const am = automaton();
for (let i = 0; i < s.length; i++) {
am.get(s[i]);
}
return am.result()
};
const MAX_NUMBER = Math.pow(2, 31) - 1;
const MIN_NUMBER = -Math.pow(2, 31);
function automaton() {
let sign = 1, ans = 0, state = "start";
let table = {
start: ["start", "signed", "in_number", "end"],
signed: ["end", "end", "in_number", "end"],
in_number: ["end", "end", "in_number", "end"],
end: ["end", "end", "end", "end"],
};
return {
get(c) {
state = table[state][this.getCol(c)];
if (state === "in_number") {
ans = ans * 10 + Number(c);
ans = sign === 1 ? Math.min(ans, MAX_NUMBER) : Math.min(ans, -MIN_NUMBER);
} else if (state === "signed") {
sign = c === "-" ? -1 : 1;
}
},
getCol(c) {
if (c == " ") {
return 0;
}
if (c == "+" || c == "-") {
return 1;
}
if (/\d/.test(c)) {
return 2;
}
return 3;
},
result() {
return sign * ans;
},
};
}