-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqrtx.rs
50 lines (43 loc) · 1.06 KB
/
sqrtx.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
struct Solution;
impl Solution {
fn impl1(x: i32) -> i32 {
(x as f64).sqrt() as i32
}
#[allow(unused)]
fn impl2(x: i32) -> i32 {
if x == 1 || x == 0 {
return x;
}
let mut ans = (x / 2) as i64;
while ans * ans > x as i64 {
ans -= 1;
}
ans as i32
}
pub fn my_sqrt(x: i32) -> i32 {
Solution::impl1(x)
}
}
fn main() {
println!("{}", Solution::my_sqrt(4));
}
#[cfg(test)]
mod tests {
use crate::Solution;
#[test]
fn test_impl1() {
assert_eq!(Solution::impl1(4), 2);
assert_eq!(Solution::impl1(8), 2);
assert_eq!(Solution::impl1(2147395599), 46339);
}
#[test]
fn test_impl2() {
assert_eq!(Solution::impl2(2), 1);
assert_eq!(Solution::impl2(1), 1);
assert_eq!(Solution::impl2(0), 0);
assert_eq!(Solution::impl2(4), 2);
assert_eq!(Solution::impl2(8), 2);
assert_eq!(Solution::impl2(2147395599), 46339);
assert_eq!(Solution::impl2(1978959248), 44485);
}
}