Skip to content

Commit 813162f

Browse files
committed
feat: update solutions to leetcode problem: No.0401. Binary Watch
1 parent 6d8e62d commit 813162f

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

solution/0400-0499/0401.Binary Watch/README.md

+20-2
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,40 @@
3535

3636
<!-- 这里可写通用的实现逻辑 -->
3737

38+
题目可转换为求 i(`i∈[0,12)`) 和 j(`j∈[0,60)`) 所有可能的组合。
39+
40+
合法组合需要满足的条件是:i 的二进制形式中 1 的个数加上 j 的二进制形式中 1 的个数,结果等于 num。
41+
3842
<!-- tabs:start -->
3943

4044
### **Python3**
4145

4246
<!-- 这里可写当前语言的特殊实现逻辑 -->
4347

4448
```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]
4652
```
4753

4854
### **Java**
4955

5056
<!-- 这里可写当前语言的特殊实现逻辑 -->
5157

5258
```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+
}
5472
```
5573

5674
### **...**

solution/0400-0499/0401.Binary Watch/README_EN.md

+16-2
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,27 @@
4141
### **Python3**
4242

4343
```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]
4547
```
4648

4749
### **Java**
4850

4951
```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+
}
5165
```
5266

5367
### **...**

solution/0400-0499/0401.Binary Watch/Solution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ public List<String> readBinaryWatch(int num) {
1010
}
1111
return res;
1212
}
13-
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
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]

0 commit comments

Comments
 (0)