原题链接

https://leetcode.com/problems/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"]

解法

public List<String> readBinaryWatch(int num) {
List<String> times = new ArrayList<>();
for (int h = 0; h < 12; h++)
for (int m = 0; m < 60; m++)
if (Integer.bitCount(h * 64 + m) == num)
return times;
}

f(n,k) = 1　　　　　　　　　　　当n=k时
f(n,k) = f(n-1,k-1) + f(n-1,k)　　　当n>k时

// 获取ledCnt个led灯，亮num个时，所有的可能性数字，不区分小时还是分钟
private List<Integer> getCombination(int ledCnt, int num) {
// 特殊处理，当没有led亮时
if(num <= 0) {
List<Integer> l = new ArrayList<Integer>();
return l;
}
// 相等时，不需要再迭代处理
if (ledCnt == num) {
List<Integer> l = new ArrayList<Integer>();
int data = 0;
for (int i = 0; i < num; i++) {
data += 1 << i;
}
return l;
} else {
List<Integer> l = new ArrayList<Integer>();

if (num > 1) {
List<Integer> subList = getCombination(ledCnt - 1, num - 1);
Integer []a = new Integer[subList.size()];
subList.toArray(a);
int high = 1 << (ledCnt - 1);
for (int i = 0; i < a.length; i++) {
}
}else {
}

return l;
}
}

int hourCnt = 4;
int minCnt = 6;
List<String> res = new ArrayList<String>();

// 小时和分钟能亮灯个数的所有可能性
for (int i = num > minCnt? (num - minCnt) : 0; i <= Math.min(hourCnt, num); i++) {
List<Integer> hourList = getCombination(hourCnt, i);
List<Integer> minList = getCombination(minCnt, num - i);
for (Integer hour : hourList) {
if (hour >= 12) {
continue;
}
for (Integer min : minList) {
if (min <= 59) {
}