-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.rs
69 lines (63 loc) · 2.36 KB
/
main.rs
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
fn main() {
println!("{}", i32::max_value());
println!("{}", i32::min_value());
println!("{}", i32::abs(i32::min_value()));
Solution::divide(10, 3);
}
struct Solution {}
impl Solution {
pub fn divide(dividend: i32, divisor: i32) -> i32 {
// deal with the only overflow case
if dividend == i32::min_value() && divisor == -1
{ return i32::max_value() }
let mut dvd = i64::abs(dividend as i64);
let dvs = i64::abs(divisor as i64);
let sign: i64 = if (dividend > 0) ^ (divisor > 0) { -1 } else { 1 };
let mut ans = 0;
while dvd >= dvs {
let mut tmp = dvs;
let mut shift: i64 = 1;
while dvd >= (tmp << 1) {
tmp <<= 1;
shift <<= 1;
}
dvd -= tmp;
ans += shift;
}
(sign * ans) as i32
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic() {
assert_eq!(Solution::divide(10, 3), 3);
assert_eq!(Solution::divide(7, -3), -2);
assert_eq!(Solution::divide(7, -7), -1);
assert_eq!(Solution::divide(7, -8), 0);
assert_eq!(Solution::divide(-7, 3), -2);
assert_eq!(Solution::divide(-7, 33), 0);
assert_eq!(Solution::divide(10, 3), 3);
}
#[test]
fn edge() {
assert_eq!(Solution::divide(i32::max_value(), i32::max_value()), 1);
assert_eq!(Solution::divide(i32::min_value(), i32::min_value()), 1);
assert_eq!(Solution::divide(i32::max_value(), i32::min_value()), 0);
assert_eq!(Solution::divide(i32::min_value(), i32::max_value()), -1);
assert_eq!(Solution::divide(i32::max_value(), 1), i32::max_value());
assert_eq!(Solution::divide(i32::min_value(), 1), i32::min_value());
assert_eq!(Solution::divide(i32::max_value(), -1), -i32::max_value());
assert_eq!(Solution::divide(i32::min_value(), -1), i32::max_value());
assert_eq!(Solution::divide( 1, i32::max_value()), 0);
assert_eq!(Solution::divide(-1, i32::max_value()), 0);
assert_eq!(Solution::divide( 1, i32::min_value()), 0);
assert_eq!(Solution::divide(-1, i32::min_value()), 0);
}
#[test]
fn leetcode() {
assert_eq!(Solution::divide(i32::min_value(), -1), i32::max_value());
assert_eq!(Solution::divide(i32::min_value(), 1), i32::min_value());
}
}