Skip to content

Commit 9c0603e

Browse files
committed
Update 0401
1 parent dd99c2c commit 9c0603e

File tree

2 files changed

+88
-48
lines changed

2 files changed

+88
-48
lines changed

leetcode/0401.Binary-Watch/401. Binary Watch.go

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,43 @@ import (
55
"strconv"
66
)
77

8+
// 解法一
9+
func readBinaryWatch(num int) []string {
10+
memo := make([]int, 60)
11+
// count the number of 1 in a binary number
12+
count := func(n int) int {
13+
if memo[n] != 0 {
14+
return memo[n]
15+
}
16+
originN, res := n, 0
17+
for n != 0 {
18+
n = n & (n - 1)
19+
res++
20+
}
21+
memo[originN] = res
22+
return res
23+
}
24+
// fmtMinute format minute 0:1 -> 0:01
25+
fmtMinute := func(m int) string {
26+
if m < 10 {
27+
return "0" + strconv.Itoa(m)
28+
}
29+
return strconv.Itoa(m)
30+
}
31+
32+
var res []string
33+
// traverse 0:00 -> 12:00
34+
for i := 0; i < 12; i++ {
35+
for j := 0; j < 60; j++ {
36+
if count(i)+count(j) == num {
37+
res = append(res, strconv.Itoa(i)+":"+fmtMinute(j))
38+
}
39+
}
40+
}
41+
return res
42+
}
43+
44+
// 解法二 打表
845
var (
946
hour = []string{"1", "2", "4", "8"}
1047
minute = []string{"01", "02", "04", "08", "16", "32"}
@@ -88,38 +125,3 @@ func findReadBinaryWatchHour(target, index int, c []int, res *[]string) {
88125
c = c[:len(c)-1]
89126
}
90127
}
91-
92-
func readBinaryWatch(num int) []string {
93-
memo := make([]int, 60)
94-
// count the number of 1 in a binary number
95-
count := func(n int) int {
96-
if memo[n] != 0 {
97-
return memo[n]
98-
}
99-
originN, res := n, 0
100-
for n != 0 {
101-
n = n & (n - 1)
102-
res++
103-
}
104-
memo[originN] = res
105-
return res
106-
}
107-
// fmtMinute format minute 0:1 -> 0:01
108-
fmtMinute := func(m int) string {
109-
if m < 10 {
110-
return "0" + strconv.Itoa(m)
111-
}
112-
return strconv.Itoa(m)
113-
}
114-
115-
var res []string
116-
// traverse 0:00 -> 12:00
117-
for i := 0; i < 12; i++ {
118-
for j := 0; j < 60; j++ {
119-
if count(i)+count(j) == num {
120-
res = append(res, strconv.Itoa(i)+":"+fmtMinute(j))
121-
}
122-
}
123-
}
124-
return res
125-
}

website/content/ChapterFour/0400~0499/0401.Binary-Watch.md

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,30 +51,67 @@ import (
5151
"strconv"
5252
)
5353

54+
// 解法一
55+
func readBinaryWatch(num int) []string {
56+
memo := make([]int, 60)
57+
// count the number of 1 in a binary number
58+
count := func(n int) int {
59+
if memo[n] != 0 {
60+
return memo[n]
61+
}
62+
originN, res := n, 0
63+
for n != 0 {
64+
n = n & (n - 1)
65+
res++
66+
}
67+
memo[originN] = res
68+
return res
69+
}
70+
// fmtMinute format minute 0:1 -> 0:01
71+
fmtMinute := func(m int) string {
72+
if m < 10 {
73+
return "0" + strconv.Itoa(m)
74+
}
75+
return strconv.Itoa(m)
76+
}
77+
78+
var res []string
79+
// traverse 0:00 -> 12:00
80+
for i := 0; i < 12; i++ {
81+
for j := 0; j < 60; j++ {
82+
if count(i)+count(j) == num {
83+
res = append(res, strconv.Itoa(i)+":"+fmtMinute(j))
84+
}
85+
}
86+
}
87+
return res
88+
}
89+
90+
// 解法二 打表
5491
var (
5592
hour = []string{"1", "2", "4", "8"}
5693
minute = []string{"01", "02", "04", "08", "16", "32"}
5794
hourMap = map[int][]string{
58-
0: []string{"0"},
59-
1: []string{"1", "2", "4", "8"},
60-
2: []string{"3", "5", "9", "6", "10"},
61-
3: []string{"7", "11"},
95+
0: {"0"},
96+
1: {"1", "2", "4", "8"},
97+
2: {"3", "5", "9", "6", "10"},
98+
3: {"7", "11"},
6299
}
63100
minuteMap = map[int][]string{
64-
0: []string{"00"},
65-
1: []string{"01", "02", "04", "08", "16", "32"},
66-
2: []string{"03", "05", "09", "17", "33", "06", "10", "18", "34", "12", "20", "36", "24", "40", "48"},
67-
3: []string{"07", "11", "19", "35", "13", "21", "37", "25", "41", "49", "14", "22", "38", "26", "42", "50", "28", "44", "52", "56"},
68-
4: []string{"15", "23", "39", "27", "43", "51", "29", "45", "53", "57", "30", "46", "54", "58"},
69-
5: []string{"31", "47", "55", "59"},
101+
0: {"00"},
102+
1: {"01", "02", "04", "08", "16", "32"},
103+
2: {"03", "05", "09", "17", "33", "06", "10", "18", "34", "12", "20", "36", "24", "40", "48"},
104+
3: {"07", "11", "19", "35", "13", "21", "37", "25", "41", "49", "14", "22", "38", "26", "42", "50", "28", "44", "52", "56"},
105+
4: {"15", "23", "39", "27", "43", "51", "29", "45", "53", "57", "30", "46", "54", "58"},
106+
5: {"31", "47", "55", "59"},
70107
}
71108
)
72109

73-
func readBinaryWatch(num int) []string {
110+
func readBinaryWatch1(num int) []string {
111+
var res []string
74112
if num > 8 {
75-
return []string{}
113+
return res
76114
}
77-
res := []string{}
78115
for i := 0; i <= num; i++ {
79116
for j := 0; j < len(hourMap[i]); j++ {
80117
for k := 0; k < len(minuteMap[num-i]); k++ {
@@ -135,6 +172,7 @@ func findReadBinaryWatchHour(target, index int, c []int, res *[]string) {
135172
}
136173
}
137174

175+
138176
```
139177

140178

0 commit comments

Comments
 (0)