LeetCode 2437. Number of Valid Clock Times

You are given a string of length 5 called time, representing the current time on a digital clock in the format "hh:mm". The earliest possible time is "00:00" and the latest possible time is "23:59".

In the string time, the digits represented by the ? symbol are unknown, and must be replaced with a digit from 0 to 9.

Return an integer answer, the number of valid clock times that can be created by replacing every ? with a digit from 0 to 9.

Example 1:

Input: time = "?5:00"
Output: 2
Explanation: We can replace the ? with either a 0 or 1, producing "05:00" or "15:00". Note that we cannot replace it with a 2, since the time "25:00" is invalid. In total, we have two choices.

Example 2:

Input: time = "0?:0?"
Output: 100
Explanation: Each ? can be replaced by any digit from 0 to 9, so we have 100 total choices.

Example 3:

Input: time = "??:??"
Output: 1440
Explanation: There are 24 possible choices for the hours, and 60 possible choices for the minutes. In total, we have 24 * 60 = 1440 choices.

Constraints:

  • time is a valid string of length 5 in the format "hh:mm".
  • "00" <= hh <= "23"
  • "00" <= mm <= "59"
  • Some of the digits might be replaced with '?' and need to be replaced with digits from 0 to 9.

又是HH:MM用?替换的题,求有多少种valid time。也就是无脑if else就行了,然而debug了好久才bug free……首先是小心char和int,然后是小心HH = ??的情况,嗯,还是需要考虑周全。

class Solution {
    public int countTime(String time) {
        int result = 1;
        char[] array = time.toCharArray();
        for (int i = 0; i < array.length; i++) {
            if (array[i] == '?') {
                if (i == 0) {
                    if (array[1] == '?') {
                        result *= 24;
                    } else if (array[1] <= '3') {
                        result *= 3;
                    } else {
                        result *= 2;
                    }
                } else if (i == 1) {
                    if (array[0] == '?') {
                        continue;
                    } else if (array[0] <= '1') {
                        result *= 10;
                    } else {
                        result *= 4;
                    }
                } else if (i == 3) {
                    result *= 6;
                } else if (i == 4) {
                    result *= 10;
                }
            }
        }
        return result;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值