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 ...
随机推荐
- VPN服务器环境搭建
一.VPN服务器环境说明 操作系统:CentOS release 6.4 (Final) 本地网卡: 复制代码 代码如下: # ifconfig em1 Link encap:Ethernet HWa ...
- 【转】四元数(Quaternion)和旋转
四元数介绍 旋转,应该是三种坐标变换--缩放.旋转和平移,中最复杂的一种了.大家应该都听过,有一种旋转的表示方法叫四元数.按照我们的习惯,我们更加熟悉的是另外两种旋转的表示方法--矩阵旋转和欧拉旋转. ...
- OAuth的机制原理讲解及开发流程
本想前段时间就把自己通过QQ OAuth1.0.OAuth2.0协议进行验证而实现QQ登录的心得及Demo实例分享给大家,可一直很忙,今天抽点时间说下OAuth1.0协议原理,及讲解下QQ对于Oaut ...
- 权限管理:(RBAC)
一般做正规的权限管理程序基本就是以下M表模式: 例1:在页面显示管理者的权限,并可以修改的管理界面 数据库表如下: 管理界面(附ajax): <body> <?php include ...
- OC文件大小的计算方法,多用于清理缓存
OC文件大小的计算方法,一般多用于清理缓存.下载.统计 可以使用以下方法: #pragma mark Bt转换 + (NSString *)axcFileSizeWithToString:(unsig ...
- MongoDB的find用法
0.查询符合条件数据的总条数 如:db.list名.find({条件}).count(); 1.返回指定的键值:db.list.find({条件},{name:"任意值",age: ...
- 在sublime中使用less
高亮显示: 可以在Less文件中显示语法高亮,这样看起来会更舒服一些. 按下Ctrl+Shift+P调出命令面板:输入install调出Install Package选项并回车:输入less,选中并安 ...
- Linux中的ps命令
Linux中ps命令用来列出系统中当前运行的那些进程. 使用格式:ps 参数 如:ps -A 通过man ps可以获得ps的详细参数用法 -A 显示所有进程信息 c 列出程序时,显示每个程序真正的 ...
- android常用命令
首先配置好环境变量会比较方便... 大部分sdk提供的工具都在sdk\platform-tools和sdk\tools下,建议配置这两个路径到path 另外aapt工具在sdk\build-tools ...
- Spring @ResponseBody只能返回String类型数据解决办法
今天自己搭Spring MVC框架玩,使用AJAX调用Spring controller 并返回map对象,突然发现,哎,怎么@Response中只能返回String, 我用的Spring 3的版本也 ...