-
Notifications
You must be signed in to change notification settings - Fork 0
/
p2609.rs
70 lines (63 loc) · 1.96 KB
/
p2609.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#[test]
fn test() {
use method2::find_the_longest_balanced_substring;
assert_eq!(
find_the_longest_balanced_substring("01000111".to_string()),
6
);
assert_eq!(find_the_longest_balanced_substring("00111".to_string()), 4);
assert_eq!(find_the_longest_balanced_substring("111".to_string()), 0);
}
// 就是个模拟题,感觉method1更清晰
// 遍历字符串,依次统计连续0的数量、连续1的数量。两者中最小的数量*2就是平衡字符串的长度
// 不断更新该长度既能得到答案
#[allow(unused)]
mod method1 {
pub fn find_the_longest_balanced_substring(s: String) -> i32 {
let mut ans: i32 = 0;
let mut i: usize = 0;
while i < s.len() {
let mut zero: i32 = 0;
let mut one: i32 = 0;
// 统计连续0的长度
while i < s.len() && &s[i..i + 1] == "0" {
zero += 1;
i += 1;
}
// 统计连续1的长度
while i < s.len() && &s[i..i + 1] == "1" {
one += 1;
i += 1;
}
let len: i32 = zero.min(one) * 2;
ans = ans.max(len);
}
ans
}
}
// 和method1思路一样,换for循环
#[allow(unused)]
mod method2 {
pub fn find_the_longest_balanced_substring(s: String) -> i32 {
let mut ans: i32 = 0;
let mut zero: i32 = 0;
let mut one: i32 = 0;
for i in 0..s.len() {
// 如果当前字符是1
if &s[i..i + 1] == "1" {
one += 1;
ans = ans.max(2 * zero.min(one));
}
// 如果当前字符是第一个字符。或者,如果当前字符是0,且是第一个0
else if i == 0 || &s[i - 1..i] == "1" {
zero = 1;
one = 0;
}
// 如果当前字符是0
else {
zero += 1;
}
}
ans
}
}