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 = 1Return: ["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".
?[暴力解法]:
時間分析:n2
空間分析:
[思維問題]:
不知道和回溯法有什么關系。一看特別麻煩,果斷用暴力解法了
[一句話思路]:
用bitCount轉化為二進制數
[輸入量]:空:?正常情況:特大:特小:程序里處理到的特殊情況:異常情況(不合法不合理的輸入):
[畫圖]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
? [五分鐘肉眼debug的結果]:
[總結]:
[復雜度]:Time complexity: O(n2) Space complexity: O(n)
[英文數據結構或算法,為什么不用別的數據結構或算法]:
麻煩
[關鍵模板化代碼]:
[其他解法]:
[Follow Up]:
[LC給出的題目變變變]:
?[代碼風格] :
public class Solution {/** @param : the number of "1"s on a given timetable* @return: all possible time*/public List<String> readBinaryWatch(int num) {List<String> time = new ArrayList<String>();for (int h = 0; h < 12; h++) {//12 not 24for (int m = 0; m < 60; m++) {if (Integer.bitCount(h) + Integer.bitCount(m) == num) {time.add(String.format("%d:%02d", h, m));//String's strict format }}}return time;} }; View Code?
轉載于:https://www.cnblogs.com/immiao0319/p/8532325.html
總結
以上是生活随笔為你收集整理的Binary Watch二进制时间的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: centos6.8_64部署django
- 下一篇: 【Flask】SelectedField