-
Notifications
You must be signed in to change notification settings - Fork 0
/
DivideTwoIntegers.kt
33 lines (25 loc) · 1 KB
/
DivideTwoIntegers.kt
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
package leetcode
/**
* Problem description on [LeetCode](https://leetcode.com/problems/divide-two-integers/)
*/
class DivideTwoIntegers {
private fun divideNegative(dividend: Int, divisor: Int): Int {
if (dividend > divisor) return 0
var sum = divisor
var count = 1
while (sum + sum in dividend..-1) {
sum += sum
count += count
}
return count + divideNegative(dividend - sum, divisor)
}
fun divide(dividend: Int, divisor: Int): Int {
if (divisor == 0) return Int.MAX_VALUE
if (divisor == -1 && dividend == Int.MIN_VALUE) return Int.MAX_VALUE
val isNegativeResult = (dividend > 0 && divisor < 0) || (dividend < 0 && divisor > 0)
val negativeDividend = if (dividend < 0) dividend else -dividend
val negativeDivisor = if (divisor < 0) divisor else -divisor
val result = divideNegative(negativeDividend, negativeDivisor)
return if (isNegativeResult) -result else result
}
}