-
Notifications
You must be signed in to change notification settings - Fork 1
/
8.字符串转换整数-atoi.ts
73 lines (63 loc) · 1.59 KB
/
8.字符串转换整数-atoi.ts
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
/*
* @lc app=leetcode.cn id=8 lang=typescript
*
* [8] 字符串转换整数 (atoi)
*/
// @lc code=start
enum State {
Start,
Signed,
InNumber,
End,
}
enum Char {
Space,
SignedChar,
Number,
Other,
}
/**
* | 当前状态 | '' | +/- | number | other |
* | ---- | ---- | ---- | ---- | ---- |
* | start | start | signed | inNumber | end |
* | signed | end | end | inNumber | end |
* | inNumber | end | end | inNumber | end |
* | end | end | end | end | end |
*/
const Table = [
/* Char.Start */ [State.Start, State.Signed, State.InNumber, State.End],
/* Char.Signed */ [State.End, State.End, State.InNumber, State.End],
/* Char.InNumber */ [State.End, State.End, State.InNumber, State.End],
/* Char.End */ [State.End, State.End, State.End, State.End],
];
function myAtoi(s: string): number {
const getStateByChar = (char: string): Char => {
if (char === " ") {
return Char.Space;
}
if (char === "-" || char === "+") {
return Char.SignedChar;
}
if (!Number.isNaN(+char)) {
return Char.Number;
}
return Char.Other;
};
let answer = 0,
sign = 1,
state: State = State.Start;
for (let index = 0; index < s.length; index++) {
const element = s[index];
state = Table[state][getStateByChar(element)];
if (state === State.InNumber) {
answer = answer * 10 + +element;
} else if (state === State.Signed) {
sign = element === "+" ? 1 : -1;
}
}
return Math.min(Math.max(answer * sign, (-2) ** 31), 2 ** 31 - 1);
}
// @lc code=end
test("myAtoi", () => {
expect(myAtoi("words and 987")).toBe(0);
});