-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
34 lines (33 loc) · 945 Bytes
/
index.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
export default function myAtoi(s: string): number {
s = s.trimStart();
if (s.length === 0) {
return 0;
}
let positive = true;
if (s.startsWith("+")) {
positive = true;
s = s.slice(1);
} else if (s.startsWith("-")) {
positive = false;
s = s.slice(1);
}
const numberchars = [];
while (intchars.includes(s[0])) {
numberchars.push(s[0]);
s = s.slice(1);
}
if (numberchars.length === 0) return 0;
// let num=Number(numberchars.join(''))
let num = 0;
for (const n of numberchars) {
num *= 10;
// num+=Number(n)
num += intchartonum.get(n) || 0;
}
if (!positive) {
num = -1 * num;
}
return Math.max(-(2 ** 31), Math.min(num, 2 ** 31 - 1));
}
const intchars = Array.from({ length: 10 }).map((_v, i) => String(i));
const intchartonum = new Map([...intchars.entries()].map(([a, b]) => [b, a]));