精通算法系列-二进制手表
来源:互联网 发布:域名证书在线生成 编辑:程序博客网 时间:2024/04/30 15:41
原题为:
// 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.// 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".
答案如下:
public class Solution { public List<String> readBinaryWatch(int num) { ArrayList<String> allTimes = new ArrayList<String>(); //iterate through all possible time combinations for(int i = 0; i < 12; i++) { for(int j = 0; j < 60; j++) { //if the current number and n have the same number of bits the time is possible if(Integer.bitCount(i * 64 + j) == num) { //add the current time to all times arraylist allTimes.add(String.format("%d:%02d", i, j)); } } } return allTimes; }}
阅读全文
0 0
- 精通算法系列-二进制手表
- Google算法题:二进制手表
- 精通算法系列-三值更小
- 精通算法系列-BST
- 精通算法系列-复制图形
- (LeetCode)Binary Watch --- 二进制手表
- 401. Binary Watch | 二进制手表
- 有趣的二进制手表问题。
- 精通算法系列-最佳买卖股票
- 精通算法系列-二叉树路径
- 精通算法系列-安卓屏幕锁
- 精通算法系列-二叉树问题
- 401. Binary Watch (二进制手表)
- [LeetCode]401. Binary Watch(二进制手表)
- leetcode 401. Binary Watch二进制手表
- 精通八大排序算法系列:二、堆排序算法
- 精通八大排序算法系列:一、快速排序算法
- 精通八大排序算法系列:二、堆排序算法
- 精通算法系列-二叉树问题
- IntelliJ IDEA 常用设置
- SpringBoot入门之基本属性配置
- 马哥视频链接lnmp
- 添加附件测试的测试点
- 精通算法系列-二进制手表
- fetch post application/json 传递复杂类型
- 微信支付大致流程
- Delphi面向对象编程的20条规则
- 索引器
- 如何通过OpenFace实现人脸识别框架
- 深度学习Day1
- Naked推智能健身镜能帮助女性更好的控制体型
- 阿里巴巴Java开发手册要点笔记 (一)