401:二进制手表
This commit is contained in:
parent
177834ce5e
commit
58348829a0
118
src/main/java/leetcode/editor/cn/BinaryWatch.java
Normal file
118
src/main/java/leetcode/editor/cn/BinaryWatch.java
Normal file
@ -0,0 +1,118 @@
|
||||
//二进制手表顶部有 4 个 LED 代表 小时(0-11),底部的 6 个 LED 代表 分钟(0-59)。每个 LED 代表一个 0 或 1,最低位在右侧。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 例如,下面的二进制手表读取 "3:25" 。
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// (图源:WikiMedia - Binary clock samui moon.jpg ,许可协议:Attribution-ShareAlike 3.0
|
||||
//Unported (CC BY-SA 3.0) )
|
||||
//
|
||||
// 给你一个整数 turnedOn ,表示当前亮着的 LED 的数量,返回二进制手表可以表示的所有可能时间。你可以 按任意顺序 返回答案。
|
||||
//
|
||||
// 小时不会以零开头:
|
||||
//
|
||||
//
|
||||
// 例如,"01:00" 是无效的时间,正确的写法应该是 "1:00" 。
|
||||
//
|
||||
//
|
||||
// 分钟必须由两位数组成,可能会以零开头:
|
||||
//
|
||||
//
|
||||
// 例如,"10:2" 是无效的时间,正确的写法应该是 "10:02" 。
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:turnedOn = 1
|
||||
//输出:["0:01","0:02","0:04","0:08","0:16","0:32","1:00","2:00","4:00","8:00"]
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:turnedOn = 9
|
||||
//输出:[]
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 0 <= turnedOn <= 10
|
||||
//
|
||||
// Related Topics 位运算 回溯 👍 356 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
//401:二进制手表
|
||||
public class BinaryWatch {
|
||||
public static void main(String[] args) {
|
||||
Solution solution = new BinaryWatch().new Solution();
|
||||
// TO TEST
|
||||
System.out.println(solution.readBinaryWatch(2));
|
||||
}
|
||||
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public List<String> readBinaryWatch(int turnedOn) {
|
||||
if (turnedOn > 8) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
Map<Integer, Set<Integer>> map1 = getNums(4, turnedOn);
|
||||
Map<Integer, Set<Integer>> map2 = getNums(6, turnedOn);
|
||||
List<String> list = new ArrayList<>();
|
||||
for (int key : map1.keySet()) {
|
||||
if (turnedOn - key >= 6) {
|
||||
continue;
|
||||
}
|
||||
for (int hour : map1.get(key)) {
|
||||
for (int mini : map2.get(turnedOn - key)) {
|
||||
list.add(hour + ":" + String.format("%02d", mini));
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private Map<Integer, Set<Integer>> getNums(int line, int turnedOn) {
|
||||
Map<Integer, Set<Integer>> map = new HashMap<>(line);
|
||||
int[] arrs = new int[line];
|
||||
arrs[0] = 1;
|
||||
for (int i = 1; i < line; i++) {
|
||||
arrs[i] = 2 * arrs[i - 1];
|
||||
}
|
||||
for (int i = 0; i <= Math.min(line, turnedOn); i++) {
|
||||
if (i == 0) {
|
||||
map.put(i, new HashSet<>(Collections.singletonList(0)));
|
||||
} else {
|
||||
Set<Integer> set = map.get(i - 1);
|
||||
Set<Integer> cur = new HashSet<>();
|
||||
for (int num : set) {
|
||||
for (int j = 0; j < line; j++) {
|
||||
if ((num & arrs[j]) == 0) {
|
||||
if (line == 4 && (num | arrs[j]) < 12) {
|
||||
cur.add(num | arrs[j]);
|
||||
} else if (line == 6 && (num | arrs[j]) < 60) {
|
||||
cur.add(num | arrs[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
map.put(i, cur);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
48
src/main/java/leetcode/editor/cn/doc/content/BinaryWatch.md
Normal file
48
src/main/java/leetcode/editor/cn/doc/content/BinaryWatch.md
Normal file
@ -0,0 +1,48 @@
|
||||
<p>二进制手表顶部有 4 个 LED 代表<strong> 小时(0-11)</strong>,底部的 6 个 LED 代表<strong> 分钟(0-59)</strong>。每个 LED 代表一个 0 或 1,最低位在右侧。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,下面的二进制手表读取 <code>"3:25"</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p><img src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2021/03/29/binary_clock_samui_moon.jpg" style="height: 300px; width" /></p>
|
||||
|
||||
<p><small><em>(图源:<a href="https://commons.m.wikimedia.org/wiki/File:Binary_clock_samui_moon.jpg">WikiMedia - Binary clock samui moon.jpg</a> ,许可协议:<a href="https://creativecommons.org/licenses/by-sa/3.0/deed.en">Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0)</a> )</em></small></p>
|
||||
|
||||
<p>给你一个整数 <code>turnedOn</code> ,表示当前亮着的 LED 的数量,返回二进制手表可以表示的所有可能时间。你可以 <strong>按任意顺序</strong> 返回答案。</p>
|
||||
|
||||
<p>小时不会以零开头:</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,<code>"01:00"</code> 是无效的时间,正确的写法应该是 <code>"1:00"</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>分钟必须由两位数组成,可能会以零开头:</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,<code>"10:2"</code> 是无效的时间,正确的写法应该是 <code>"10:02"</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>turnedOn = 1
|
||||
<strong>输出:</strong>["0:01","0:02","0:04","0:08","0:16","0:32","1:00","2:00","4:00","8:00"]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>turnedOn = 9
|
||||
<strong>输出:</strong>[]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= turnedOn <= 10</code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>位运算</li><li>回溯</li></div></div><br><div><li>👍 356</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user