leetcode_401_Binary Watch_回溯法_java实现
题目:
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".
解题思路:
回溯法特点:
1.递归
2.有树形结构,对问题进行遍历
3.最重要的一点就是碰到不符合的条件的分支停止递归,进行回溯。
4.得到最终结果进行回溯,再遍历下一种情况
* 本题递归:
* 从问题抽象到子问题:
* 问题:在0到9这10个位置上可以点亮n盏灯
* 操作:在位置i(0=<i<=9)上点亮一盏灯
* 操作后得到子问题:在i+1到9这些位置上可以点亮n-1盏灯
*
* 递归结束条件:n==0,当灯已经都点亮时,得到一个最终解,递归终止,逐步返回,同时也逐步回溯,把所有沿途点亮的灯都灭掉。
有一个小疑问点说明一下:例如当n=7,i=7时,也就是,当剩下的位置数小于还需要点亮的灯的数量时,这时候程序依然可以正确运行
因为这时候不等n减少到0,i 就会首先增大到大于9,for循环就会不执行,递归就会一层一层传递回来。
代码:
public static List<String> readBinaryWatch(int num) {
List<String> times = new ArrayList<String>(); //时间集合
int[] watch = new int[10]; //数组表示灯的位置,下标为 0到3 表示 小时的最高位到最低位
//下标为4到9表示分钟的最高位到最低位。
//例如:watch[0]的权是8,watch[3]的权是1,watch[4]的权是32,watch[9]的权是1
Trial(num, 0, watch, times);
return times;
}
//n代表亮灯的数量,a代表现在可亮灯位置的第一位(最后一位是 9)
//这个代表的含义是:在位置(以数组下标表示)a到9 可以点亮n盏灯,
//第一次调用时a=0,代表的意思是在数组下标 0到9 可以点亮n盏灯
public static void Trial(int n,int a,int[] watch,List<String> times){
if(n==0) {
String time = "";
int hour = SumOfIntArray(3, 0, watch);
time = time + hour + ":";
int minute = SumOfIntArray(9, 4, watch);
if(minute<10) time = time+0+minute;
else time = time + minute;
times.add(time);
return; //递归返回,这句话关键代码,一定不要忘了!!!
//但是本程序这行不加不影响,大不了不返回上一层,接着执行,
//n减小成负数,a不停增加,直到大于9,再逐步返回。
}
for(;a<=9;a++){
watch[a] = 1;//在a位置点亮一盏灯
//当不是下面这些情况时,递归循环,如果是的话,不进入循环,直接回溯
if( ! ((watch[0] == 1 && watch[1] == 1) ||
(watch[4] == 1 && watch[5] == 1 && watch[6] == 1 && watch[7] == 1))){
//进入递归循环,子问题的意思是:在位置(以数组下标表示)a+1到9 可以点亮n-1盏灯
Trial(n-1, a+1, watch, times);
}
watch[a]=0; //回溯
}
return;
}
public static int SumOfIntArray(int a,int b,int[] times){
//a是低位,b是高位,a是3或9,b是0或4
int sum = 0;
int mi = 0;
for(int i=a;i>=b;i--){
sum = (int) (sum +times[i]*Math.pow(2, mi));
mi++;
}
return sum;
}
leetcode_401_Binary Watch_回溯法_java实现的更多相关文章
- 回溯法解决N皇后问题(以四皇后为例)
以4皇后为例,其他的N皇后问题以此类推.所谓4皇后问题就是求解如何在4×4的棋盘上无冲突的摆放4个皇后棋子.在国际象棋中,皇后的移动方式为横竖交叉的,因此在任意一个皇后所在位置的水平.竖直.以及45度 ...
- uva216 c++回溯法
因为题目要求最多8台电脑,所以可以枚举全排列,然后依次计算距离进行比较,枚举量8!=40320并不大,但这种方法不如回溯法好,当数据再大一些枚举就显得笨拙了,所以这个题我用回溯法做的,回溯有一个好处是 ...
- UVa 129 (回溯法) Krypton Factor
回溯法确实不是很好理解掌握的,学习紫书的代码细细体会. #include <cstdio> ]; int n, L, cnt; int dfs(int cur) { if(cnt++ == ...
- 实现n皇后问题(回溯法)
/*======================================== 功能:实现n皇后问题,这里实现4皇后问题 算法:回溯法 ============================= ...
- UVA - 524 Prime Ring Problem(dfs回溯法)
UVA - 524 Prime Ring Problem Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
- HDU 2553 n皇后问题(回溯法)
DFS Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description ...
- HDU 1016 Prime Ring Problem (回溯法)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 八皇后问题-回溯法(MATLAB)
原创文章,转载请注明:八皇后问题-回溯法(MATLAB) By Lucio.Yang 1.问题描述 八皇后问题是十九世纪著名数学家高斯于1850年提出的.问题是:在8*8的棋盘上摆放8个皇后,使其不能 ...
- 使用回溯法求所有从n个元素中取m个元素的组合
不多说了,直接上代码,代码中有注释,应该不难看懂. #include <stdlib.h> #include <stdio.h> typedef char ELE_TYPE; ...
随机推荐
- 重新想象 Windows 8 Store Apps (65) - 后台任务: 音乐的后台播放和控制
[源码下载] 重新想象 Windows 8 Store Apps (65) - 后台任务: 音乐的后台播放和控制 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 后台 ...
- ASP.NET或WinFrom中获取汉子的拼音首字母
1.获得一个字符串的每个字的拼音首字母构成所需的字符串 #region 获取首字母 /// <summary> /// 这个办法是用来获得一个字符串的每个字的拼音首字母构 ...
- 技巧题---Single boy
Description Today is Christmas day. There are n single boys standing in a line. They are numbered fo ...
- 平衡二叉树---Shaolin
Description Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shaolin temp ...
- php生成静态文件
1,通用生成方法 //获取文件内容 $content=file_get_contents("http://www.google.com/" ); $id=110; $filenam ...
- js填写银行卡号,每隔4位数字加一个空格
1.原生js写法 !function () { document.getElementById('bankCard').onkeyup = function (event) { var v = thi ...
- GetReadyForWin10Develop
GetReadyForWin10Develop 序言 今年4月29日晚的微软的Build大会上,微软在现场为我们演示了Android和IOS应用移植到windows平台,加上原本可以开发win8应用的 ...
- andriod Spinner
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout android:orientatio ...
- Android 身份证号码查询、手机号码查询、天气查询
1.基本信息 身份证号码查询:http://apistore.baidu.com/apiworks/servicedetail/113.html 手机号码:http://apistore.baidu. ...
- tableview直接滚动至最后一行的问题
tableview直接滚动至最后一行 类似聊天界面,tableview应该直接显示在最后一行,并且不应该有滚动的出现. 在网上查了很久,直接滚动至最后一行很容易实现,有两种方法比较好. 1. 调用sc ...