Leetcode: Binary Watch
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".
Solution 1: Bit Manipulation
use Integer.bitCount()
public class Solution {
public List<String> readBinaryWatch(int num) {
List<String> res = new ArrayList<String>();
for (int i=0; i<12; i++) {
for (int j=0; j<60; j++) {
if (Integer.bitCount(i) + Integer.bitCount(j) == num) {
String str1 = Integer.toString(i);
String str2 = Integer.toString(j);
res.add(str1 + ":" + (j<10? "0"+str2 : str2));
}
}
}
return res;
}
}
Solution 2: Backtracking, 非常精妙之处在于用了两个数组来帮助generate digit(例如:1011 -> 11)
public class Solution {
public List<String> readBinaryWatch(int num) {
int[] nums1 = new int[]{8, 4, 2, 1}, nums2 = new int[]{32, 16, 8, 4, 2, 1};
List<String> res = new ArrayList<String>();
for (int i=0; i<=num; i++) {
List<Integer> hours = getTime(nums1, i, 12);
List<Integer> minutes = getTime(nums2, num-i, 60);
for (int hour : hours) {
for (int minute : minutes) {
res.add(hour + ":" + (minute<10? "0"+minute : minute));
}
}
}
return res;
}
public List<Integer> getTime(int[] nums, int count, int limit) {
List<Integer> res = new ArrayList<Integer>();
getTimeHelper(res, count, 0, 0, nums, limit);
return res;
}
public void getTimeHelper(List<Integer> res, int count, int pos, int sum, int[] nums, int limit) {
if (count == 0) {
if (sum < limit)
res.add(sum);
return;
}
for (int i=pos; i<nums.length; i++) {
getTimeHelper(res, count-1, i+1, sum+nums[i], nums, limit);
}
}
}
Leetcode: Binary Watch的更多相关文章
- LeetCode:Binary Tree Level Order Traversal I II
LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...
- LeetCode: Binary Tree Traversal
LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...
- [LeetCode] Binary Search 二分搜索法
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...
- LeetCode Binary Search All In One
LeetCode Binary Search All In One Binary Search 二分查找算法 https://leetcode-cn.com/problems/binary-searc ...
- LeetCode & Binary Search 解题模版
LeetCode & Binary Search 解题模版 In computer science, binary search, also known as half-interval se ...
- [LeetCode] Binary Watch 二进制表
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom ...
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [LeetCode] Binary Tree Right Side View 二叉树的右侧视图
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
随机推荐
- vbaexcel
Sub WordTest() Dim objwordApp As Word.Application Dim objword As Word.Document Dim objSeheet As Stri ...
- 1 2 3 n
n(n+1)/2 连续自然数 1,2,3.....,n 队列 从中任意取出1至n个相加,可以表示的连续自然数队列中最大的自然数是多少 受"高斯求和--蛇头蛇尾脑图--长方形对角线脑图--苯环 ...
- DML以及DQL的使用方法
DML:数据操作语言 1.插入insert into 单行插入:insert into 表名 (字段名, 字段名,...) values (值, 值, ...) 注:值列表要和字段列表相匹配. ins ...
- springMVC配置文件spring-servlet.xml中<mvc:annotation-driven />的意义
<mvc:annotation-driven/>标签,对应的实现类是org.springframework.web.servlet.config.AnnotationDrivenBeanD ...
- 【No.4 Ionic】修改 cordova 插件
在使用 cordova 过程 使用的插件 有可能不能满足个人需求,就需要修改,下面就直接说说步骤 插件结构 我用 cordova-plugin-inappbrowser 插件 讲解 在目录中有个 sr ...
- VB动态添加WebBrowser控件,并拦截弹出窗口(不用引用任何组件)
新建空白窗体,然后粘帖下面代码: Option ExplicitPublic WithEvents br As VBControlExtender Private Sub br_ObjectEvent ...
- Bluetooth L2CAP介绍
目录 1. 通用操作 1. L2CAP Channel 2. 设备间操作 3. 层间操作 4. 操作模式 2. 数据包格式(Data Packet Format) 1. B-Frame 2. G-Fr ...
- 关于网站的UV分析
一:准备 1.统计的维度 guid tracktime provice 2.key与value的设定 key:date+provice_guid value:NullWritable 3.案例分析 表 ...
- Qt中单例模式的实现(4种方法)
最简单的写法: 12345 static MyClass* MyClass::Instance(){ static MyClass inst; return &inst;} 过去很长一段时间一 ...
- 【Android开发学习笔记】【第一课】初识New Project,工程文件介绍
初学者新建一个Andriod工程后,往往不知道Pakage Explorer区域的每个文件是什么作用,今天学习了一下,自我总结一下. 1.先新建一个工程 2.输入名称,以及支持的SDK版本等(这些可以 ...
