1.回溯题。递归实现.
class Solution {
//public static List<String> list=new ArrayList<String>();
public List<String> readBinaryWatch(int num) {
List<String> list=new ArrayList<String>();
int[] a=new int[4];
int[] b=new int[6];
Arrays.fill(a,0);
Arrays.fill(b,0);
backTracking(list,0,0,num,a,b);
return list;
}
public static void backTracking(List<String> list,int hour,int minute,int num,int[] a,int[] b) {
int[] a2=new int[4];
int[] b2=new int[6];
for(int i=0;i<a.length;i++) {
a2[i]=a[i];
}
for(int i=0;i<b.length;i++) {
b2[i]=b[i];
}
if(num==0) {
String s1=String.valueOf(hour);
String s2=String .valueOf(minute);
if(minute<10) {
s2="0"+s2;
}
list.add(s1+":"+s2);
}else {
for(int i=0;i<b2.length;i++) {
if(b2[i]==0) {
if(minute+Math.pow(2, i)<=59) {
b2[i]=1;
backTracking(list,hour,minute+(int)(Math.pow(2, i)),num-1,a2,b2);
}
}
}
for(int i=0;i<a2.length;i++) {
if(a2[i]==0) {
if(Math.pow(2, i)+hour<=11) {
a2[i]=1;
backTracking(list,hour+(int)(Math.pow(2, i)),minute,num-1,a2,b2);
}
}
}
}
}
}