File tree Expand file tree Collapse file tree 4 files changed +40
-5
lines changed
solution/0400-0499/0401.Binary Watch Expand file tree Collapse file tree 4 files changed +40
-5
lines changed Original file line number Diff line number Diff line change 35
35
36
36
<!-- 这里可写通用的实现逻辑 -->
37
37
38
+ 题目可转换为求 i(`i∈[0,12)`) 和 j(`j∈[0,60)`) 所有可能的组合。
39
+
40
+ 合法组合需要满足的条件是:i 的二进制形式中 1 的个数加上 j 的二进制形式中 1 的个数,结果等于 num。
41
+
38
42
<!-- tabs:start -->
39
43
40
44
### **Python3**
41
45
42
46
<!-- 这里可写当前语言的特殊实现逻辑 -->
43
47
44
48
```python
45
-
49
+ class Solution:
50
+ def readBinaryWatch(self, num: int) -> List[str]:
51
+ return ['{:d}:{:02d}'.format(i, j) for i in range(12) for j in range(60) if (bin(i) + bin(j)).count('1') == num]
46
52
```
47
53
48
54
### **Java**
49
55
50
56
<!-- 这里可写当前语言的特殊实现逻辑 -->
51
57
52
58
```java
53
-
59
+ class Solution {
60
+ public List<String> readBinaryWatch(int num) {
61
+ List<String> res = new ArrayList<>();
62
+ for (int i = 0; i < 12; ++i) {
63
+ for (int j = 0; j < 60; ++j) {
64
+ if (Integer.bitCount(i) + Integer.bitCount(j) == num) {
65
+ res.add(String.format("%d:%02d", i, j));
66
+ }
67
+ }
68
+ }
69
+ return res;
70
+ }
71
+ }
54
72
```
55
73
56
74
### **...**
Original file line number Diff line number Diff line change 41
41
### **Python3**
42
42
43
43
```python
44
-
44
+ class Solution:
45
+ def readBinaryWatch(self, num: int) -> List[str]:
46
+ return ['{:d}:{:02d}'.format(i, j) for i in range(12) for j in range(60) if (bin(i) + bin(j)).count('1') == num]
45
47
```
46
48
47
49
### **Java**
48
50
49
51
```java
50
-
52
+ class Solution {
53
+ public List<String> readBinaryWatch(int num) {
54
+ List<String> res = new ArrayList<>();
55
+ for (int i = 0; i < 12; ++i) {
56
+ for (int j = 0; j < 60; ++j) {
57
+ if (Integer.bitCount(i) + Integer.bitCount(j) == num) {
58
+ res.add(String.format("%d:%02d", i, j));
59
+ }
60
+ }
61
+ }
62
+ return res;
63
+ }
64
+ }
51
65
```
52
66
53
67
### **...**
Original file line number Diff line number Diff line change @@ -10,4 +10,4 @@ public List<String> readBinaryWatch(int num) {
10
10
}
11
11
return res;
12
12
}
13
- }
13
+ }
Original file line number Diff line number Diff line change
1
+ class Solution:
2
+ def readBinaryWatch(self, num: int) -> List[str]:
3
+ return ['{:d}:{:02d}'.format(i, j) for i in range(12) for j in range(60) if (bin(i) + bin(j)).count('1') == num]
You can’t perform that action at this time.
0 commit comments