• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

fxss5201 / fxss-leetcode / 13518554010

25 Feb 2025 10:01AM UTC coverage: 96.565% (-0.2%) from 96.788%
13518554010

push

github

fxss5201
feat: add 8. 字符串转换整数 (atoi)

847 of 908 branches covered (93.28%)

Branch coverage included in aggregate %.

58 of 66 new or added lines in 2 files covered. (87.88%)

3117 of 3197 relevant lines covered (97.5%)

5.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.05
/src/leetcode/stringToIntegerAtoi/typescript.ts
1
// 8. 字符串转换整数 (atoi):https://leetcode.cn/problems/string-to-integer-atoi/
2
// 输入:" -042"
3
// 输出:-42
4

5
export function stringToIntegerAtoi (s: string): number {
1✔
6
  let res = 0
7✔
7
  let sign = 1
7✔
8
  let i = 0
7✔
9
  const len = s.length
7✔
10
  while (i < len && s[i] === ' ') {
7✔
11
    i++
1✔
12
  }
1✔
13
  if (i < len && s[i] === '-') {
7✔
14
    sign = -1
2✔
15
    i++
2✔
16
  }
2✔
17
  if (i < len && s[i] === '+') {
7✔
18
    if (sign === -1) return 0
2✔
19
    i++
1✔
20
  }
1✔
21
  while (i < len && s[i] >= '0' && s[i] <= '9') {
7✔
22
    res = res * 10 + Number(s[i])
12✔
23
    i++
12✔
24
  }
12✔
25
  res = res * sign
6✔
26
  const intMax = 2 ** 31 - 1
6✔
27
  if (res > intMax) {
7!
NEW
28
    return intMax
×
NEW
29
  }
✔
30
  const intMin = -(2 ** 31)
6✔
31
  if (res < intMin) {
7!
NEW
32
    return intMin
×
NEW
33
  }
✔
34
  return res
6✔
35
}
6✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc