761. 特殊的二进制字符串

This commit is contained in:
li_chx 2026-02-20 16:22:44 +08:00
parent 54bea679f0
commit 0f894b29a8
Signed by: li_chx
GPG Key ID: 70D4985BB8180E92
1 changed files with 27 additions and 18 deletions

View File

@ -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;
}
}
arr.sort_unstable_by(|a,b| b.cmp(a));
arr.join("")
}
}
if index == 1e5 as usize {
letters[0]
} else {
letters[index]
}
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);
}