LeetCode:二进制手表【401】
LeetCode:二进制手表【401】
题目描述
二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59)。
每个 LED 代表一个 0 或 1,最低位在右侧。

例如,上面的二进制手表读取 “3:25”。
给定一个非负整数 n 代表当前 LED 亮着的数量,返回所有可能的时间。
案例:
输入: n = 1
返回: ["1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", "0:16", "0:32"]
注意事项:
- 输出的顺序没有要求。
- 小时不会以零开头,比如 “01:00” 是不允许的,应为 “1:00”。
- 分钟必须由两位数组成,可能会以零开头,比如 “10:2” 是无效的,应为 “10:02”。
题目分析
这道题目标注为简单题,但感觉做起来比较费劲,虽然最后写出来了,但感觉代码很冗余。讨论有很多非常巧妙的办法,需要运用数学方法,我是绝对想不出来的。
我的思路是这样的,题目意思是在小时数组{1,2,4,8} 和分钟数组{1,2,4,8,16,32}中一共选择N个数来组成一个时间。
那我们第一个要解决的问题是如何在一个数组中选出所有N个数的组合。
1、如何取出组合的第一个数?
我们从左往右,首先我们将a加入tmpList(临时存储排列的线性表)中,此后再从它下一个位置开始找第二个、第三个数字,最后生成排列存储在结果中。我们再将1作为第一个元素开始处理后,赶紧把他删了,再将b加入到tmpList中,此后再从它下一个位置开始找第二个、第三个数字,最后生成排列存储在结果中....
也就是说处理第i个位置的元素时,就要考虑到i位置的所有取值,我们在处理为a的元素后,赶紧把他删了处理为b的元素.....这样第i个位置就有所有的情况了。

知道这个以后,我们就可以得到在一个数组中选出所有N个数的组合。
2、关于排列组合的一个典型的递归回溯框架是这样的
private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums){
if(tempList.size() == nums.length){
list.add(new ArrayList<>(tempList));
} else{
for(int i = 0; i < nums.length; i++){
if(tempList.contains(nums[i])) continue; // element already exists, skip
tempList.add(nums[i]);
backtrack(list, tempList, nums);
tempList.remove(tempList.size() - 1);
}
}
}
当我们分别取出小时和分钟的各种可能情况,再次做他们的笛卡尔积,从而生产所有的时间可能。
很抱歉的是,这道题并不需要严格的递归回溯框架,我从而使用了简单使用了深搜。
/**
*
* @param arr 待产生排列的数组
* @param index 位置下标
* @param cur 已经找了CUR个数
* @param n 一共要找N个数
* @param val 已经找到的数的和
* @param res 当招够N个数后把他加入res表中
* @param max val不能超过多少
*/
private void helper(int[] arr,int index,int cur,int n,int val,List<Integer> res,int max)
{
if(cur>n)
return;
if(cur==n&&val<max)
res.add(val);
for(int i =index;i<arr.length;i++) {
helper(arr, i+1,cur+1, n, val + arr[i],res,max);
}
}
Java题解
class Solution {
public List<String> readBinaryWatch(int num) {
int[] hour = {1,2,4,8};
int[] minute ={1,2,4,8,16,32};
List<Integer> hourList = new ArrayList<>();
List<Integer> minuteList = new ArrayList<>();
List<String> ans = new ArrayList<>();
for(int i=0;i<=num;i++)
{
helper(hour,0,0,i,0,hourList,12);
helper(minute,0,0,num-i,0,minuteList,60);
for(int hi = 0;hi<hourList.size();hi++)
for(int mi =0;mi<minuteList.size();mi++)
{
String minVal = String.valueOf(minuteList.get(mi));
if(minVal.length()<2)
minVal = "0"+minVal;
ans.add(hourList.get(hi)+":"+minVal);
}
hourList = new ArrayList<>();
minuteList = new ArrayList<>();
}
Collections.sort(ans);
return ans;
}
private void helper(int[] arr,int index,int cur,int n,int val,List<Integer> res,int max)
{
if(cur>n)
return;
if(cur==n&&val<max)
res.add(val);
for(int i =index;i<arr.length;i++) {
helper(arr, i+1,cur+1, n, val + arr[i],res,max);
}
}
}
LeetCode:二进制手表【401】的更多相关文章
- Java实现 LeetCode 401 二进制手表
401. 二进制手表 二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右侧. 例如,上面的二进制手表 ...
- 【leetcode 简单】 第九十三题 二进制手表
二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右侧. 例如,上面的二进制手表读取 “3:25”. ...
- [Swift]LeetCode401. 二进制手表 | Binary Watch
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom ...
- Leetcode401Binary Watch二进制手表
二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右侧. 给定一个非负整数 n 代表当前 LED 亮着 ...
- leetcode -- 二进制
leetcode -- 二进制 在学习编程语言的运算符时,大部分语言都会有与,或等二进制运算符,我在初期学习这些运算符的时候,并没有重点留意这些运算符,并且在后续的业务代码中也没有频繁的使用过,直到后 ...
- 401 Binary Watch 二进制手表
详见:https://leetcode.com/problems/binary-watch/description/ C++: class Solution { public: vector<s ...
- LeetCode 二进制问题
338. Counting Bits(计算小于n的各个数值对应的二进制1的个数) 思路:通过奇偶判断,if i是偶数,a[i]=a[i/2],if i是奇数,a[i]=a[i-1]+1. class ...
- leetcode 二进制求和 python
class Solution: def addBinary(self, a, b): """ :type a: str :type b: str :rtype: str ...
- [LeetCode&Python] Problem 401. Binary Watch
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom ...
随机推荐
- python+pandas+openpyxl下载xls illegalCharacterError
仅仅是urllib2.unquote_plus解码是不够的,需要将特殊字符去掉 ILLEGAL_CHARACTERS_RE = re.compile(r'[\000-\010]|[\013-\014] ...
- DevelopmentValue
DevelopmentValue mysql为utf8为什么网页返回数据及写入mysql数据库均为乱码? eclipse运行配了maven之后,创建包也弹出这个错误,每次都弹 c语言怎么建立txt文件 ...
- NDK版本 下载地址
最新版本r16 https://dl.google.com/android/repository/android-ndk-r16-windows-x86.zip https://dl.google.c ...
- MFC多国语言——资源DLL
此随笔中主要内容来自http://blog.csdn.net/china_hxx/article/details/10066655,原出处不详. 以下内容基于VC 6.0.要实现界面多语言化,必须要先 ...
- node.js中的事件循环机制
http://www.cnblogs.com/dolphinX/p/3475090.html
- java -Mac搭建本地服务器并映射到外网
最近在学习Html,小有进步变想着写一个浪漫的静态页面给女朋友浪漫一下,那么问题就来了,如何把我的网页让对网络一窍不通的女朋友看到,所以便想到了是用自己电脑作为服务器的想法.百度以后整理如下: 首先搭 ...
- 你一定喜欢看的 Webpack 2.× 入门实战
from:https://www.jianshu.com/p/b83a251d53db?utm_campaign=maleskine&utm_content=note&utm_medi ...
- Android无线测试之—UiAutomator UiObject API介绍七
判断对象是否存在 1.判断对象是否存在相关API 返回值 API 描述 boolean waitForExists(long timeout) 等待对象出现 boolean waitUntilGone ...
- UITabBarItem如何更改高度
本文转载至 http://www.cocoachina.com/bbs/read.php?tid=255361 我目前有个UITabBar,改了它的高度.但是我切换页签后,这个UITabBar样式又变 ...
- Linux下自动调整时间和时区与Internet时间同步
(原文链接) 调整linux系统时间和时区与Internet时间同步一.修改时区:# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime修改为中国的 ...