diff --git a/src/main.rs b/src/main.rs index 778f4c9..a8822d0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,45 +1,27 @@ struct Solution; mod arr; impl Solution { - fn get_min(num: i32) -> i32 { - let mut chars: Vec = vec![]; - let mut write_in = false; - let mut one_count = 0; - for c in format!("{:b}", num).chars().rev() { - if write_in { - chars.push(c); - continue; - } - if c == '0' { - if one_count == 0 { - return -1; + pub fn next_greatest_letter(letters: Vec, 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; + } } - for _ in 0..one_count -1 { - chars.push('1'); - } - one_count = 0; - chars.push('0'); - chars.push(c); - write_in = true; - continue; } - one_count += 1; } - let mut ans = 0; - if one_count > 0 { - return num >> 1; + if index == 1e5 as usize { + letters[0] + } else { + letters[index] } - for c in chars.into_iter().rev() { - ans = ans * 2 + c as i32 - '0' as i32; - } - ans - } - pub fn min_bitwise_array(nums: Vec) -> Vec { - nums.into_iter().map(|x| Solution::get_min(x)).collect() } } - 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); }