-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbroken_calc.rs
54 lines (44 loc) · 1.06 KB
/
broken_calc.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
// 991. Broken Calculator, Medium
// https://leetcode.com/problems/broken-calculator/
impl Solution {
pub fn broken_calc(start_value: i32, target: i32) -> i32 {
let mut ops = 0;
let mut value = target;
while value > start_value {
if value % 2 == 1 {
value += 1;
ops += 1;
}
value /= 2;
ops += 1;
}
ops += start_value - value;
ops
}
}
struct Solution {}
#[cfg(test)]
mod tests {
use super::*;
use crate::vec_vec_i32;
#[test]
fn test_broken_calc() {
assert_eq!(Solution::broken_calc(2, 3), 2);
}
#[test]
fn test_broken_calc2() {
assert_eq!(Solution::broken_calc(5, 8), 2);
}
#[test]
fn test_broken_calc3() {
assert_eq!(Solution::broken_calc(3, 10), 3);
}
#[test]
fn test_broken_calc4() {
assert_eq!(Solution::broken_calc(1024, 1), 1023);
}
#[test]
fn test_broken_calc5() {
assert_eq!(Solution::broken_calc(1, 1000000000), 39);
}
}