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 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### ** ...**
Original file line number Diff line number Diff line change 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### ** ...**
Original file line number Diff line number Diff line change @@ -10,4 +10,4 @@ public List<String> readBinaryWatch(int num) {
1010 }
1111 return res ;
1212 }
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