LeetCode 401. Binary Watch
2017-06-14 12:25
369 查看
题目描述:
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).
Each LED represents a zero or one, with the least significant bit on the right.
![](https://upload.wikimedia.org/wikipedia/commons/8/8b/Binary_clock_samui_moon.jpg)
For example, the above binary watch reads "3:25".
Given a non-negative integer n which represents the number of LEDs that are currently on, return all possible times the watch could represent.
Example:
Note:
The order of output does not matter.
The hour must not contain a leading zero, for example "01:00" is not valid, it should be "1:00".
The minute must be consist of two digits and may contain a leading zero, for example "10:2" is not valid, it should be "10:02".
思路:有2个思路
1、固定灯数,尝试所有的分配(时针h,分针num-h),用回溯法求解出所有时针和分针的可能能,然后全排列
2、遍历所有时间(0:00~11:59),输出其二进制中‘1’的个数和为num的时间
第一种方法比较复杂,需要用回溯算法,回溯的目标是实现list中任意k个数的和。(因为本题值比较少,也可以使用穷举)
第二种方法比较简单,可以用python oneline实现,
Solution1:
Solution2(python):
return ["%d:%02d"%(h,m) for h in range(12) for m in range(60) if (bin(h)+bin(m)).count('1') == x]
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).
Each LED represents a zero or one, with the least significant bit on the right.
![](https://upload.wikimedia.org/wikipedia/commons/8/8b/Binary_clock_samui_moon.jpg)
For example, the above binary watch reads "3:25".
Given a non-negative integer n which represents the number of LEDs that are currently on, return all possible times the watch could represent.
Example:
Input: n = 1 Return: ["1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", "0:16", "0:32"]
Note:
The order of output does not matter.
The hour must not contain a leading zero, for example "01:00" is not valid, it should be "1:00".
The minute must be consist of two digits and may contain a leading zero, for example "10:2" is not valid, it should be "10:02".
思路:有2个思路
1、固定灯数,尝试所有的分配(时针h,分针num-h),用回溯法求解出所有时针和分针的可能能,然后全排列
2、遍历所有时间(0:00~11:59),输出其二进制中‘1’的个数和为num的时间
第一种方法比较复杂,需要用回溯算法,回溯的目标是实现list中任意k个数的和。(因为本题值比较少,也可以使用穷举)
第二种方法比较简单,可以用python oneline实现,
Solution1:
class Solution { public: string conver2str(int x) { stringstream ss; ss << x; string s = ss.str(); return s; } vector<string> readBinaryWatch(int num) { vector<int> hours = { 1,2,4,8 }; vector<int> minutes = { 1,2,4,8,16,32 }; vector<string> res; for (int h = 0; h <= 4 && h <= num; h++) { vector<int> possibleH = possibleValus(hours, h); vector<int> possibleM = possibleValus(minutes, num - h); for (int i=0;i<possibleH.size();i++){ if(possibleH[i]>11) continue; for (int j = 0; j < possibleM.size(); j++) { if(possibleM[j]<60){ string sh = conver2str(possibleH[i]); string sm = conver2str(possibleM[j]); if (possibleM[j] < 10) sm = "0" + sm; res.push_back(sh + ":" + sm); } } } } return res; } void getSum(vector<int> l, int count, int pos, int sum, vector<int> &res) { if (count == 0) { res.push_back(sum); return; } for (int i = pos; i < l.size(); i++) { getSum(l, count - 1, i + 1, sum + l[i], res); } } vector<int> possibleValus(vector<int> l, int n) { vector<int> res; getSum(l, n, 0, 0, res); return res; } };
Solution2(python):
return ["%d:%02d"%(h,m) for h in range(12) for m in range(60) if (bin(h)+bin(m)).count('1') == x]
相关文章推荐
- [leetcode: Python]401. Binary Watch
- Leetcode-401. Binary Watch
- leetCode 401. Binary Watch
- 【LeetCode】401. Binary Watch
- Leetcode: 401. Binary Watch
- [LeetCode]401. Binary Watch &bitset的用法&to_string
- LeetCode-401. Binary Watch
- [LeetCode]401. Binary Watch
- 位操作-leetcode 401. Binary Watch
- 【Leetcode】401. Binary Watch
- leetcode 401. Binary Watch
- [leetcode]: 401. Binary Watch
- Leetcode 401. Binary Watch
- LeetCode-Backtracking-401. Binary Watch
- 401. Binary Watch
- 401. Binary Watch
- 401. Binary Watch
- 401. Binary Watch
- 401. Binary Watch
- 401. Binary Watch