Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e749c2f

Browse files
author
guangsheng.li01
committed
Solved 0761
1 parent 30a04ff commit e749c2f

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

‎src/a0761_special_binary_string.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* [0761] special-binary-string
3+
*/
4+
5+
struct Solution;
6+
7+
impl Solution {
8+
pub fn make_largest_special(s: String) -> String {
9+
let s = s.as_bytes();
10+
let mut l = Vec::new();
11+
let mut start = 0;
12+
let mut count = 0;
13+
for i in 0..s.len() {
14+
count += if s[i] as char == '1' { 1 } else { -1 };
15+
if count == 0 {
16+
let substr = std::str::from_utf8(&s[start + 1..i]).unwrap().to_string();
17+
l.push("1".to_string() + &Self::make_largest_special(substr) + "0");
18+
start = i + 1;
19+
}
20+
}
21+
l.sort();
22+
l.reverse();
23+
l.concat()
24+
}
25+
}
26+
27+
#[cfg(test)]
28+
mod tests {
29+
use super::*;
30+
31+
#[test]
32+
fn test_case0() {
33+
assert_eq!(
34+
Solution::make_largest_special("11011000".to_owned()),
35+
"11100100".to_owned()
36+
);
37+
}
38+
}

‎src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ mod a0643_maximum_average_subarray_i;
3434
mod a0646_maximum_length_of_pair_chain;
3535
mod a0654_maximum_binary_tree;
3636
mod a0701_insert_into_a_binary_search_tree;
37+
mod a0761_special_binary_string;
3738
mod a0867_transpose_matrix;
3839
mod a0883_projection_area_of_3d_shapes;
3940
mod a0894_all_possible_full_binary_trees;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /