761. 特殊的二进制字符串
This commit is contained in:
parent
54bea679f0
commit
0f894b29a8
43
src/main.rs
43
src/main.rs
|
|
@ -1,27 +1,36 @@
|
|||
struct Solution;
|
||||
mod arr;
|
||||
|
||||
impl Solution {
|
||||
pub fn next_greatest_letter(letters: Vec<char>, target: char) -> char {
|
||||
let mut index = 1e5 as usize;
|
||||
for i in 0..letters.len() {
|
||||
if letters[i] > target {
|
||||
if index == 1e5 as usize {
|
||||
index = i;
|
||||
} else {
|
||||
if letters[i] < letters[index] {
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
pub fn sp(s : &str)-> String{
|
||||
if s.len() <= 2 {
|
||||
return s.to_string();
|
||||
}
|
||||
let mut left = 0;
|
||||
let mut cnt = 0;
|
||||
let sb = s.as_bytes();
|
||||
let mut arr = Vec::new();
|
||||
for i in 0..s.len() {
|
||||
if sb[i] == b'1' {
|
||||
cnt += 1;
|
||||
}else {
|
||||
cnt -= 1;
|
||||
}
|
||||
if cnt == 0 {
|
||||
arr.push(format!("1{}0",Self::sp(&s[left + 1 .. i])));
|
||||
left = i + 1;
|
||||
}
|
||||
}
|
||||
if index == 1e5 as usize {
|
||||
letters[0]
|
||||
} else {
|
||||
letters[index]
|
||||
}
|
||||
arr.sort_unstable_by(|a,b| b.cmp(a));
|
||||
arr.join("")
|
||||
}
|
||||
pub fn make_largest_special(s: String) -> String {
|
||||
Self::sp(s.as_str())
|
||||
}
|
||||
}
|
||||
fn main() {
|
||||
let result = Solution::next_greatest_letter(vec!['c', 'f', 'j'], 'a');
|
||||
let result = Solution::make_largest_special(
|
||||
"101101101010101010001010101010110011011010001100".to_string(),
|
||||
);
|
||||
println!("{:?}", result);
|
||||
}
|
||||
Loading…
Reference in New Issue