Last active
February 21, 2020 22:25
-
-
Save jakab922/4b7783b0d5f618e41c34e9adea4cdb96 to your computer and use it in GitHub Desktop.
Solution of https://leetcode.com/problems/next-closest-time/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
impl Solution { | |
pub fn next_closest_time(time: String) -> String { | |
let vec: Vec<char> = time.chars().filter(|&x| x != ':').collect(); | |
let to_usize = |x| vec[x] as usize - 48; | |
let base_value = 60 * (10 * to_usize(0) + to_usize(1)) + 10 * to_usize(2) + to_usize(3); | |
let mut letters = vec![false; 10]; | |
for c in vec.iter() { | |
let i = *c as usize - 48; | |
letters[i] = true; | |
} | |
let mut numbers: Vec<bool> = (0..60).map(|i| letters[i % 10] && letters[i / 10]).collect(); | |
let mut current_value = (base_value + 1) % 2400; | |
loop { | |
let (first, second) = (current_value / 60, current_value % 60); | |
if first < 24 && second < 60 && numbers[first] && numbers[second] { | |
return format!("{:02}:{:02}", first, second); | |
} | |
current_value = (current_value + 1) % 2400; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment