Skip to content

Instantly share code, notes, and snippets.

@jakab922
Last active February 21, 2020 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakab922/4b7783b0d5f618e41c34e9adea4cdb96 to your computer and use it in GitHub Desktop.
Save jakab922/4b7783b0d5f618e41c34e9adea4cdb96 to your computer and use it in GitHub Desktop.
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