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 ...
随机推荐
- MP20 MBO issue summary
MP3 MBO经验,教训 改名字HGA003_PTOT_01,发现居然闪现进度条,正常情况是不会闪现进度条的,只是改个名字而已,怀疑之前用过这个名字,所以我后来改成HGA003_PTOT_03了. 先 ...
- changing a pointer rather than erasing memory cells
Computer Science An Overview _J. Glenn Brookshear _11th Edition In a data structure based on a point ...
- w_click_twice
var w_global_obj; var dat = new Date(); var w_golbal_count_millseconds; function w_click_twice(w_cur ...
- OpenGL 完全教程(写给Delphi的开发者) 前言
前言 在开发了许多2D图形程序之后,许多人开始对3D图形编程产生了兴趣.学习一套3D API,是进行3D图形编程的基础.在有趣的3D图形编程中,3D API只不过充当着一种低级的工具而已.因此,在这里 ...
- Address localhost:1099 is already in use 的错误
http://blog.csdn.net/huazhongkejidaxuezpp/article/details/41813683
- 在Vista或更高版本Windows系统中, 获取超大图标的办法
这几天写个小东西, 需要获取系统正在运行的程序图标, 一般来说32*32就足够了, 不过既然Win7能够支持超大图标(256*256), 咱们也需要与时俱进, 说不定什么时候遇到个变态客户就有这要求了 ...
- Gradle 修改 Maven 仓库地址
gradle install--- http://www.itnose.net/detail/6500082.html http://stackoverflow.com/questions/51025 ...
- 读书笔记——《图解TCP/IP》(4/4)
经典摘抄 第八章 应用层协议概要 1.应用协议是为了实现某种应用而设计和创造的协议. 2.TCP/IP的应用层包含了管理通信连接的会话层功能.转换数据格式的表示层功能,还包括与对端主机交互的应用层功能 ...
- Qt high DPI
http://doc.qt.io/qt-5/highdpi.html Qt Support Ability to provide pixmaps or artwork for high resolut ...
- ArcGIS API for Silverlight 绘制降雨路径动画
原文:ArcGIS API for Silverlight 绘制降雨路径动画 #region 降雨动画演示 2014-04-16 List<Graphic> graphics = new ...
