2437.有效时间的数目
链接:2437.有效时间的数目
难度:Easy
标签:字符串、枚举
简介:请你返回一个整数 answer ,将每一个 ? 都用 0 到 9 中一个数字替换后,可以得到的有效时间的数目。
题解 1 - cpp
- 编辑时间:2023-05-09
- 内存消耗:5.9MB
- 编程语言:cpp
- 解法介绍:枚举。
class Solution {
public:
    int countTime(string time) {
        vector<int> idxs;
        for (int i = 0; i < time.size(); i++) {
            if (time[i] == '?') idxs.push_back(i);
        }
        int res = 0;
        if (idxs.empty()) {
            return check(time) ? 1 : 0;
        }
        function<void(int, string)> dfs = [&](int idx, string time) {
            if (idx == idxs.size()) {
                if (check(time)) res++;
                return;
            }
            for (int i = 0; i <= 9; i++) {
                time[idxs[idx]] = i + '0';
                dfs(idx + 1, time);
            }
        };
        dfs(0, time);
        return res;
    }
    bool check(string &time) {
        int h = (time[0] - '0') * 10 + (time[1] - '0'), 
            m = (time[3] - '0') * 10 + (time[4] - '0'); 
        if (h >= 24 || m >= 60) return false;
        return true;
    }
};
题解 2 - python
- 编辑时间:2023-05-09
- 执行用时:48ms
- 内存消耗:15.9MB
- 编程语言:python
- 解法介绍:同上。
class Solution:
    def countTime(self, time: str) -> int:
        time = list(time)
        def check(time: str) -> bool:
            h = (ord(time[0]) - ord('0')) * 10 + ord(time[1]) - ord('0')
            m = (ord(time[3]) - ord('0')) * 10 + ord(time[4]) - ord('0')
            return h < 24 and m < 60
        idxs = []
        for i in range(len(time)):
            if time[i] == '?':
                idxs.append(i)
        if len(idxs) == 0:
            return 1 if check(time) else 0
        res = 0
        def dfs(idx: int, time: List[str]):
            nonlocal res
            if idx == len(idxs):
                if check(time):
                    res += 1
            else:
                for i in range(0, 10):
                    time[idxs[idx]] = chr(i + ord('0'))
                    dfs(idx+1, time)
        dfs(0, time)
        return res
题解 3 - rust
- 编辑时间:2023-05-09
- 内存消耗:1.9MB
- 编程语言:rust
- 解法介绍:同上。
fn str_to_vec(s: &String) -> Vec<char> {
    s.chars().collect()
}
impl Solution {
    pub fn count_time(time: String) -> i32 {
        let time = str_to_vec(&time);
        let mut idxs = vec![];
        for i in 0..time.len() {
            if time[i] == '?' {
                idxs.push(i);
            }
        }
        if idxs.is_empty() {
            if Solution::check(&time) {
                1
            } else {
                0
            }
        } else {
            let mut res = 0;
            Solution::dfs(&mut res, &idxs, 0, time);
            res
        }
    }
    fn check(time: &Vec<char>) -> bool {
        let h = (time[0] as u8 - b'0') * 10 + (time[1] as u8 - b'0');
        let m = (time[3] as u8 - b'0') * 10 + (time[4] as u8 - b'0');
        h < 24 && m < 60
    }
    fn dfs(res: &mut i32, idxs: &Vec<usize>, idx: usize, mut time: Vec<char>) {
        if idx == idxs.len() {
            if Solution::check(&time) {
                *res += 1;
            }
        } else {
            for i in 0..10 {
                time[idxs[idx]] = (i + b'0') as char;
                Solution::dfs(res, idxs, idx + 1, time.clone());
            }
        }
    }
}