publicclassSolution { public IList<string> ReadBinaryWatch(int turnedOn) { IList<String> ans = new List<String>(); for (int h = 0; h < 12; ++h) { for (int m = 0; m < 60; ++m) { if (BitCount(h) + BitCount(m) == turnedOn) { ans.Add(h + ":" + (m < 10 ? "0" : "") + m); } } } return ans; }
privatestaticintBitCount(int i) { i = i - ((i >> 1) & 0x55555555); i = (i & 0x33333333) + ((i >> 2) & 0x33333333); i = (i + (i >> 4)) & 0x0f0f0f0f; i = i + (i >> 8); i = i + (i >> 16); return i & 0x3f; } }
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10
funcreadBinaryWatch(turnedOn int) (ans []string) { for h := uint8(0); h < 12; h++ { for m := uint8(0); m < 60; m++ { if bits.OnesCount8(h)+bits.OnesCount8(m) == turnedOn { ans = append(ans, fmt.Sprintf("%d:%02d", h, m)) } } } return }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11
var readBinaryWatch = function(turnedOn) { const ans = []; for (let h = 0; h < 12; ++h) { for (let m = 0; m < 60; ++m) { if (h.toString(2).split('0').join('').length + m.toString(2).split('0').join('').length === turnedOn) { ans.push(h + ":" + (m < 10 ? "0" : "") + m); } } } return ans; };
[sol1-Python3]
1 2 3 4 5 6 7 8
classSolution: defreadBinaryWatch(self, turnedOn: int) -> List[str]: ans = list() for h inrange(12): for m inrange(60): ifbin(h).count("1") + bin(m).count("1") == turnedOn: ans.append(f"{h}:{m:02d}") return ans
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
char** readBinaryWatch(int turnedOn, int* returnSize) { char** ans = malloc(sizeof(char*) * 12 * 60); *returnSize = 0; for (int h = 0; h < 12; ++h) { for (int m = 0; m < 60; ++m) { if (__builtin_popcount(h) + __builtin_popcount(m) == turnedOn) { char* tmp = malloc(sizeof(char) * 6); sprintf(tmp, "%d:%02d", h, m); ans[(*returnSize)++] = tmp; } } } return ans; }
publicclassSolution { public IList<string> ReadBinaryWatch(int turnedOn) { IList<String> ans = new List<String>(); for (int i = 0; i < 1024; ++i) { int h = i >> 6, m = i & 63; // 用位运算取出高 4 位和低 6 位 if (h < 12 && m < 60 && BitCount(i) == turnedOn) { ans.Add(h + ":" + (m < 10 ? "0" : "") + m); } } return ans; }
privatestaticintBitCount(int i) { i = i - ((i >> 1) & 0x55555555); i = (i & 0x33333333) + ((i >> 2) & 0x33333333); i = (i + (i >> 4)) & 0x0f0f0f0f; i = i + (i >> 8); i = i + (i >> 16); return i & 0x3f; } }
[sol2-Golang]
1 2 3 4 5 6 7 8 9
funcreadBinaryWatch(turnedOn int) (ans []string) { for i := 0; i < 1024; i++ { h, m := i>>6, i&63// 用位运算取出高 4 位和低 6 位 if h < 12 && m < 60 && bits.OnesCount(uint(i)) == turnedOn { ans = append(ans, fmt.Sprintf("%d:%02d", h, m)) } } return }
[sol2-JavaScript]
1 2 3 4 5 6 7 8 9 10
var readBinaryWatch = function(turnedOn) { const ans = []; for (let i = 0; i < 1024; ++i) { let h = i >> 6, m = i & 63; // 用位运算取出高 4 位和低 6 位 if (h < 12 && m < 60 && i.toString(2).split('0').join('').length === turnedOn) { ans.push(h + ":" + (m < 10 ? "0" : "") + m); } } return ans; };
[sol2-Python3]
1 2 3 4 5 6 7 8
classSolution: defreadBinaryWatch(self, turnedOn: int) -> List[str]: ans = list() for i inrange(1024): h, m = i >> 6, i & 0x3f# 用位运算取出高 4 位和低 6 位 if h < 12and m < 60andbin(i).count("1") == turnedOn: ans.append(f"{h}:{m:02d}") return ans
[sol2-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
char** readBinaryWatch(int turnedOn, int* returnSize) { char** ans = malloc(sizeof(char*) * 12 * 60); *returnSize = 0; for (int i = 0; i < 1024; ++i) { int h = i >> 6, m = i & 63; // 用位运算取出高 4 位和低 6 位 if (h < 12 && m < 60 && __builtin_popcount(i) == turnedOn) { char* tmp = malloc(sizeof(char) * 6); sprintf(tmp, "%d:%02d", h, m); ans[(*returnSize)++] = tmp; } }