日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Binary Watch二进制时间

發(fā)布時間:2025/4/14 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Binary Watch二进制时间 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

[抄題]:

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".

?[暴力解法]:

時間分析:n2

空間分析:

[思維問題]:

不知道和回溯法有什么關系。一看特別麻煩,果斷用暴力解法了

[一句話思路]:

用bitCount轉(zhuǎn)化為二進制數(shù)

[輸入量]:空:?正常情況:特大:特小:程序里處理到的特殊情況:異常情況(不合法不合理的輸入):

[畫圖]:

[一刷]:

  • 表的小時不超過12
  • String.format嚴格控制字符串格式
  • [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

    ? [五分鐘肉眼debug的結(jié)果]:

    [總結(jié)]:

    [復雜度]:Time complexity: O(n2) Space complexity: O(n)

    [英文數(shù)據(jù)結(jié)構(gòu)或算法,為什么不用別的數(shù)據(jù)結(jié)構(gòu)或算法]:

    麻煩

    [關鍵模板化代碼]:

    [其他解法]:

    [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

    ?

    轉(zhuǎn)載于:https://www.cnblogs.com/immiao0319/p/8532325.html

    總結(jié)

    以上是生活随笔為你收集整理的Binary Watch二进制时间的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。