LC.P2437[有效时间的数目]

方法一:乘法原理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public int countTime(String time) {
return count(time.substring(0, 2), 24) * count(time.substring(3, 5), 60);
}

private int count(String time, int x) {
int ans = 0;
char[] s = time.toCharArray();
for (int i = 0; i < x; ++i) {
if ((s[0] == '?' || i / 10 == s[0] - '0') && (s[1] == '?' || i % 10 == s[1] - '0')) {
++ans;
}
}
return ans;
}
}
  • 时间复杂度:$O(24 + 60) = O(1)$
  • 空间复杂度:$O(1)$