Skip to content

Commit 77ca029

Browse files
committed
Upsert 1784-check-if-binary-string-has-at-most-one-segment-of-ones.rs
1 parent 3283e59 commit 77ca029

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub fn check_ones_segment(s: String) -> bool {
2+
if s == "1".to_string() { return true }
3+
let cs = s.chars().collect::<Vec<char>>();
4+
let mut prev_char = '1';
5+
let mut change_times = 0;
6+
for c in &cs[1..] {
7+
if *c != prev_char { change_times += 1 }
8+
prev_char = *c;
9+
}
10+
change_times <= 1
11+
}
12+
13+
fn main() {
14+
println!("{:?}", check_ones_segment("1001".to_string())); // false
15+
println!("{:?}", check_ones_segment("110".to_string())); // true
16+
println!("{:?}", check_ones_segment("1".to_string())); // true
17+
println!("{:?}", check_ones_segment("10".to_string())); // true
18+
}

0 commit comments

Comments
 (0)