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

fxss5201 / fxss-leetcode / 13535219254

26 Feb 2025 02:27AM UTC coverage: 96.352% (-0.2%) from 96.565%
13535219254

push

github

fxss5201
feat: add 29. 两数相除

878 of 947 branches covered (92.71%)

Branch coverage included in aggregate %.

99 of 105 new or added lines in 2 files covered. (94.29%)

3216 of 3302 relevant lines covered (97.4%)

5.76 hits per line

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

89.86
/src/leetcode/divideTwoIntegers/typescript.ts
1
// 29. 两数相除:https://leetcode.cn/problems/divide-two-integers/
2
// 输入:divideTwoIntegers(10, 3)
3
// 输出:3
4

5
export function divideTwoIntegers (dividend: number, divisor: number): number {
1✔
6
  const intMax = 2 ** 31 - 1
2✔
7
  const intMin = -(2 ** 31)
2✔
8
  if (dividend === intMin) {
2!
NEW
9
    if (divisor === 1) return intMin
×
NEW
10
    if (divisor === -1) return intMax
×
NEW
11
  }
×
12
  if (divisor === intMin) return dividend === intMin ? 1 : 0
2!
13
  if (dividend === 0) return 0
2!
14
  let rev = false
2✔
15
  if (dividend > 0) {
2✔
16
    dividend = -dividend
2✔
17
    rev = !rev
2✔
18
  }
2✔
19
  if (divisor > 0) {
2✔
20
    divisor = -divisor
1✔
21
    rev = !rev
1✔
22
  }
1✔
23
  let left = 1
2✔
24
  let right = intMax
2✔
25
  let ans = 0
2✔
26
  while (left <= right) {
2✔
27
    const mid = left + ((right - left) >> 1)
62✔
28
    const check = quickAdd(divisor, mid, dividend)
62✔
29
    if (check) {
62✔
30
      ans = mid
3✔
31
      if (mid === intMax) break
3!
32
      left = mid + 1
3✔
33
    } else {
62✔
34
      right = mid - 1
59✔
35
    }
59✔
36
  }
62✔
37
  return rev ? -ans : ans
2✔
38

39
  function quickAdd(y: number, z: number, x: number): boolean {
2✔
40
    let result = 0
62✔
41
    let add = y
62✔
42
    while (z !== 0) {
62✔
43
      if ((z & 1) !== 0) {
124✔
44
        if (result < x - add) return false
6✔
45
        result += add
5✔
46
      }
5✔
47
      if (z !== 1) {
124✔
48
        if (add < x - add) return false
120✔
49
        add += add
62✔
50
      }
62✔
51
      z >>= 1
65✔
52
    }
65✔
53
    return true
3✔
54
  }
62✔
55
}
2✔
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