Easy
410692FavoriteShare
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".
dfs小时分钟一起法:
Java:
import java.util.ArrayList;
import java.util.List;
/*
* @Autor: SourDumplings
* @Date: 2019-09-22 08:58:07
* @Link: https://github.com/SourDumplings/
* @Email: changzheng300@foxmail.com
* @Description: https://leetcode.com/problems/binary-watch/
*
* 用正数表示小时,负数表示分钟,放在一起dfs
*/
class Solution
{
private int[] power =
{ 1, 2, 4, 8, -1, -2, -4, -8, -16, -32 };
public List<String> readBinaryWatch(int num)
{
List<String> res = new ArrayList<>();
if (num == 0)
{
res.add("0:00");
}
else
{
for (int i = 0; i < 10; i++)
{
dfs(res, i, num - 1, 0, 0);
}
}
return res;
}
public void dfs(List<String> res, int b, int n, int h, int m)
{
int value = power[b];
if (value < 0)
{
m -= value;
}
else
{
h += value;
}
if (n == 0)
{
if (h < 12 && m < 60)
{
process(res, h, m);
}
}
else
{
for (int i = b + 1; i < 10; i++)
{
dfs(res, i, n - 1, h, m);
}
}
if (value < 0)
{
m += value;
}
else
{
h -= value;
}
}
public void process(List<String> res, int h, int m)
{
StringBuilder sBuilder = new StringBuilder();
sBuilder.append(String.valueOf(h));
sBuilder.append(":");
if (m < 10)
{
sBuilder.append("0");
}
sBuilder.append(String.valueOf(m));
res.add(sBuilder.toString());
}
}
dfs小时分钟分开法:
C++:
/*
* @Autor: SourDumplings
* @Date: 2019-09-22 07:06:42
* @Link: https://github.com/SourDumplings/
* @Email: changzheng300@foxmail.com
* @Description: https://leetcode.com/problems/binary-watch/
*
*/
class Solution
{
private:
int power[6] = {1, 2, 4, 8, 16, 32};
public:
vector<string> readBinaryWatch(int num)
{
vector<string> res;
if (num == 0)
{
res.push_back("0:00");
}
else
{
for (int h = 0; h <= num; h++)
{
if (h < 4 && num - h < 6 && num - h >= 0) // 不可能亮6个或以上分钟灯,也不能亮4个或以上的小时灯
{
combine(res, h, num - h);
}
}
}
return res;
}
void combine(vector<string> &res, int h, int m)
{
vector<int> hours, minutes;
if (h > 0)
{
for (int i = 0; i < 4; i++)
{
dfs(hours, h - 1, i, 4, 0);
}
}
else
{
hours.push_back(0);
}
if (m > 0)
{
for (int i = 0; i < 6; i++)
{
dfs(minutes, m - 1, i, 6, 0);
}
}
else
{
minutes.push_back(0);
}
int hNum = hours.size();
int mNum = minutes.size();
for (int i = 0; i < hNum; i++)
{
for (int j = 0; j < mNum; j++)
{
process(res, hours[i], minutes[j]);
}
}
}
void dfs(vector<int> &num, int n, int b, int e, int value)
{
value += power[b];
if (n == 0)
{
num.push_back(value);
}
else
{
for (int i = b + 1; i < e; i++)
{
dfs(num, n - 1, i, e, value);
}
}
value -= power[n];
}
void process(vector<string> &res, int h, int m)
{
if (h >= 12 || m >= 60)
{
return;
}
char minutes[4];
sprintf(minutes, ":%02d", m);
res.push_back(to_string(h) + minutes);
}
};