744. 寻找比目标字母大的最小字母
This commit is contained in:
parent
873bf741e6
commit
54bea679f0
56
src/main.rs
56
src/main.rs
|
|
@ -1,45 +1,27 @@
|
||||||
struct Solution;
|
struct Solution;
|
||||||
mod arr;
|
mod arr;
|
||||||
impl Solution {
|
impl Solution {
|
||||||
fn get_min(num: i32) -> i32 {
|
pub fn next_greatest_letter(letters: Vec<char>, target: char) -> char {
|
||||||
let mut chars: Vec<char> = vec![];
|
let mut index = 1e5 as usize;
|
||||||
let mut write_in = false;
|
for i in 0..letters.len() {
|
||||||
let mut one_count = 0;
|
if letters[i] > target {
|
||||||
for c in format!("{:b}", num).chars().rev() {
|
if index == 1e5 as usize {
|
||||||
if write_in {
|
index = i;
|
||||||
chars.push(c);
|
} else {
|
||||||
continue;
|
if letters[i] < letters[index] {
|
||||||
}
|
index = i;
|
||||||
if c == '0' {
|
}
|
||||||
if one_count == 0 {
|
}
|
||||||
return -1;
|
}
|
||||||
}
|
}
|
||||||
for _ in 0..one_count -1 {
|
if index == 1e5 as usize {
|
||||||
chars.push('1');
|
letters[0]
|
||||||
}
|
} else {
|
||||||
one_count = 0;
|
letters[index]
|
||||||
chars.push('0');
|
}
|
||||||
chars.push(c);
|
|
||||||
write_in = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
one_count += 1;
|
|
||||||
}
|
|
||||||
let mut ans = 0;
|
|
||||||
if one_count > 0 {
|
|
||||||
return num >> 1;
|
|
||||||
}
|
|
||||||
for c in chars.into_iter().rev() {
|
|
||||||
ans = ans * 2 + c as i32 - '0' as i32;
|
|
||||||
}
|
|
||||||
ans
|
|
||||||
}
|
|
||||||
pub fn min_bitwise_array(nums: Vec<i32>) -> Vec<i32> {
|
|
||||||
nums.into_iter().map(|x| Solution::get_min(x)).collect()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let result = Solution::min_bitwise_array(vec![11,13,31]);
|
let result = Solution::next_greatest_letter(vec!['c', 'f', 'j'], 'a');
|
||||||
println!("{:?}", result);
|
println!("{:?}", result);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue