MCPcopy Create free account
hub / github.com/StudyRust/leetcode_rust / count_binary_substrings

Function count_binary_substrings

696-count-binary-substrings.rs:6–31  ·  view source on GitHub ↗
(s: String)

Source from the content-addressed store, hash-verified

4// 取小的一个加起来,就是3+2+2 = 7
5
6pub fn count_binary_substrings(s: String) -> i32 {
7 let mut cur_char = 'w';
8 let mut tmp_count = 0;
9 let mut arr = vec!();
10 for c in s.chars() {
11 if c == cur_char {
12 tmp_count += 1;
13 } else {
14 if tmp_count != 0 {
15 arr.push(tmp_count);
16 }
17 cur_char = c;
18 tmp_count = 1;
19 }
20 }
21 arr.push(tmp_count);
22 let mut res = 0;
23 for i in 0..(&arr.len()-1) {
24 if arr[i] < arr[i+1] {
25 res += arr[i];
26 } else {
27 res += arr[i+1];
28 }
29 }
30 res
31}
32
33fn main() {
34 let s = "00110011".to_string();

Callers

nothing calls this directly

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected