[LeetCode]401. Binary Watch
来源:互联网 发布:网络主播直播内容规划 编辑:程序博客网 时间:2024/05/16 06:09
题目大意:点击打开链接
思路:把手表上的每个LED灯看做一个二进制位,如果灯是亮的就代表1,不用在意LED灯的位置,转化为计算一共有多少个给定数字的组合(参考题目给的Example 1,num=1),并在一个List<String>里打印出来这些组合。
public class Solution { public List<String> readBinaryWatch(int num) { List<String> result=new ArrayList<>(); for(int i=0;i<12;i++){ for(int j=0;j<60;j++){ if(Integer.bitCount(i)+Integer.bitCount(j)==num){ result.add(String.format("%d:%02d",i,j)); } } } return result; }}
要注意的就是最后的字符串输出形式,依题目要求,时针位不能有前导零,分针位必须加前导零。
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
- 401. [LeetCode]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
- datatables 学习(一)
- HBuilder 使用教程
- pyh 修改addJS时默认的位置
- 我们分析了200款“二次元游戏”,最终发现了这些
- xcode8升级cocoapad
- [LeetCode]401. Binary Watch
- 什么是元数据(Metadata)
- 重写QAbstractItemModel
- 保存图片到【相机胶卷】和【自定义相册】
- eclipse 导入hadoop源码
- FastJson 栽坑汇总
- 启动Tomcat时一闪而过(Tomcat启动失败)
- How To Load CLOB Data from a File into a CLOB column using PL/SQL
- Windows10 64bit 系统下安装 Anaconda3(Python3)的Theano