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; ...
随机推荐
- asp.net mvc UpdateModel 更新对象后出现null
在用asp.net mvc 4.0做项目的时候遇到的这种情况 在填写表单的时候,有一些表单没有填写,留空,然后直接post 提交表单,action中用UpdateModel 来更新model, 结果发 ...
- wrong requestcode when using startActivityForResult
You are calling startActivityForResult() from your Fragment. When you do this, the requestCode is ch ...
- css样式reset
初始化样式. @CHARSET "UTF-8"; body,ur,ol,li,p,h1,h2,h3,h4,h5,h6,form,fieldset,table,td,img,div, ...
- 蒙特卡洛树搜索算法(UCT): 一个程序猿进化的故事
前言: 本文是根据的文章Introduction to Monte Carlo Tree Search by Jeff Bradberry所写. Jeff Bradberry还提供了一整套的例子,用p ...
- php学习笔记:自定义函数的调用
PHP内置了超过1000个函数,因此函数使得PHP成为一门非常强大的语言.大多数时候我们使用系统的内置函数就可以满足需求,但是自定义函数通过将一组代码封装起来,使代码进行复用,程序结构与逻辑更加清晰. ...
- ASP.NET Web API涉及到的上下文
1.表示请求的上下文:HttpRequestContext; 2.表示HttpController:HttpControllerContext; 3.表示Action方法:HttpActionCont ...
- React入门--------JSX
React学习网站 React官方英文网站:http://reactjs.cn/react/docs/top-level-api.html React官方中文网站:http://www.css88.c ...
- javascript作用域链学习笔记
作用域链 "JavaScript中的函数运行在它们被定义的作用域里,而不是它们被执行的作用域里." --权威指南 在JavaScript中,一切皆对象,包括函数.函数对象和其它对象 ...
- Failed to upgrade AX 2012 R3 Retail channel database from CU9 to CU11 if SQL Server version was lower than 2012
I tried to upgrade AX 2012 R3 Retail channel database from CU9 to CU11 for client. after generated n ...
- 初识UIScrollView
RootView.m #import "RootView.h" #define YHColor [UIColor colorWithRed:arc4random() % 256 / ...