位操作-leetcode 401. Binary Watch
来源:互联网 发布:中邮网络培训学院 编辑:程序博客网 时间:2024/05/29 13:44
原题链接:Binary Watch
题解:
class Solution {public: //计算1的位数 int countBits(int n){ if(n==0)return 0; int count=1; while(n=n&(n-1))count++; return count; } vector<string> readBinaryWatch(int num) { /* Time Complexity:O(1) Space Complexity:O(1) */ vector<string>svec; if(num<0 || num>8 )return svec; if(num==0)return vector<string>{"0:00"}; vector<int>hours; for(int i=0;i<=3&&i<=num;i++){ hours.clear(); for(int temp=0;temp<=11;temp++){ if(countBits(temp)==i){ hours.push_back(temp); } } for(int temp=0;temp<=59;temp++){ if(countBits(temp)==num-i){ for(int k=0;k<hours.size();k++){ svec.push_back(to_string(hours[k])+":"+((temp<10)?("0"+to_string(temp)):to_string(temp))); } } } } return svec; }};
阅读全文
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
- 401. [LeetCode]Binary Watch
- Leetcode 401. Binary Watch
- Leetcode: 401. Binary Watch
- LeetCode 401. Binary Watch
- 【Leetcode】401. Binary Watch
- Leetcode-401. Binary Watch
- 怎么面试架构师
- F1V3.0-图形-绘图快捷操作帮助
- CDOJ1590-dfs+树链剖分(2017 UESTC Training for Data Structures O)
- Window 对象
- 利用递归求n以内的素数之和
- 位操作-leetcode 401. Binary Watch
- Android中EditText限制仅允许输入汉字/仅允许汉字和数字
- 百度ueditor富文本--PC端单个,PC端多个,mobile单个,mobile多个
- win7 codeblock在调用boost::thread遇到的错误——解决办法
- 我的第一个Hbuilder项目(WEB)
- 照片人脸建模
- Linux 升级 Python 至 3.x
- 我的第一个hbuilder项目
- You may need an appropriate loader to handle this file type