Leetcode Binary Watch
来源:互联网 发布:js 读取ios沙盒文件 编辑:程序博客网 时间:2024/06/07 03:14
题意:手表用二进制显示时间,给出亮灯的个数,求可能的时间。
思路:枚举子集的个数。这里数量较小,直接用二进制枚举。
class Solution {public: vector<string> readBinaryWatch(int num) { vector<int> h(4, 1); for(int i = 1; i < h.size(); i ++ ) { h[i] = h[i - 1] * 2; } vector<int> m(6, 1); for(int i = 1; i < m.size(); ++ i) { m[i] = m[i - 1] * 2; } vector<string> re; vector<int> myh, mym; for(int i = 0; i <= num; ++ i) { myh = dfsH(h, i); mym = dfsM(m, num - i); for(int j = 0; j < myh.size(); j ++) { string hour = std::to_string(myh[j]); for(int k = 0; k < mym.size(); k ++) { string minutes; if(mym[k] < 10) minutes = "0" + std::to_string(mym[k]); else minutes = std::to_string(mym[k]); re.push_back(hour + ":" + minutes); } } } return re; } vector<int> dfsH(vector<int> h, int nums) { vector<int> re; //int total = 7; for(int i = 0; i <= 15; ++ i) { if(ones(i) == nums && sum(h, i) < 12) { re.push_back(sum(h, i)); } } return re; } vector<int> dfsM(vector<int> h, int nums) { vector<int> re; //int total = 127; for(int i = 0; i <= 127; ++ i) { if(ones(i) == nums && sum(h, i) < 60) { //cout << i << " " << ones(i) << " " << sum(h, i) << endl; re.push_back(sum(h, i)); } } return re; } int ones(int n) { int cout = 0; while(n) { cout += n%2; n /= 2; } return cout; } int sum(vector<int> n, int ind) { int re = 0; int i = 0; while(ind) { if(i >= n.size()) return 1000; if(ind % 2) { re += n[i]; } ind /= 2; i ++; //if(i >= n.size()) return 1000; } return re; }};
0 0
- 【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 401 Binary Watch
- Leetcode 401. Binary Watch
- LeetCode 401. Binary Watch
- LeetCode 401. Binary Watch
- LeetCode 401. Binary Watch
- leetcode 401. Binary Watch
- Leetcode Binary Watch 401
- 【LeetCode】 401. Binary Watch
- [leetcode][401]. Binary Watch
- [LeetCode]401. Binary Watch
- 401. [LeetCode]Binary Watch
- Android设计中的.9.png图片
- JQueryEasyUI 组件 布局 portal组件
- 基本概念
- java.unit工具类的使用(Arrays类进行排序,查找;Random生成随机数)
- 数据结构—图II
- Leetcode Binary Watch
- CentOS_6.7脚本静默安装MySQL5.6
- Vi语法总结
- JQueryEasyUI 组件 datagrid组件(数据表格组件)
- 数据结构 — 查找I
- 485. Max Consecutive Ones
- SPOJ 纽约大学ACM第一题TEST
- Android应用主界面Tab切换框架详解之Fragment
- 数据结构 — 查找II