Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5f9c14c

Browse files
committed
src/bin/first-bad-version.rs
1 parent 440b746 commit 5f9c14c

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/bin/first-bad-version.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
fn main() {
2+
// println!("{}", Solution::new(1702766719).first_bad_version(2126753390));
3+
// println!("{}", Solution::new(1).first_bad_version(1));
4+
println!("{}", Solution::new(2).first_bad_version(2));
5+
}
6+
7+
struct Solution {
8+
n: i32
9+
}
10+
11+
// The API isBadVersion is defined for you.
12+
// isBadVersion(versions:i32)-> bool;
13+
// to call it use self.isBadVersion(versions)
14+
15+
impl Solution {
16+
fn new(n: i32) -> Self { Self { n } }
17+
18+
pub fn first_bad_version(&self, n: i32) -> i32 {
19+
let mut good: i64 = 0;
20+
let mut bad: i64 = n as i64;
21+
while bad - good > 1 {
22+
let mid = (good + bad + 1) / 2;
23+
if self.isBadVersion(mid as i32) {
24+
bad = mid;
25+
} else {
26+
good = mid;
27+
}
28+
}
29+
bad as i32
30+
}
31+
32+
fn isBadVersion(&self, versions: i32) -> bool {
33+
self.n <= versions
34+
}
35+
}

0 commit comments

Comments
 (0)