加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

[Swift]LeetCode401. 二进制手表 | Binary Watch

发布时间:2020-12-14 05:11:09 所属栏目:百科 来源:网络整理
导读: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 wa

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

?二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59)。

每个 LED 代表一个 0 或 1,最低位在右侧。

例如,上面的二进制手表读取 “3:25”。

给定一个非负整数?n?代表当前 LED 亮着的数量,返回所有可能的时间。

案例:

输入: n = 1
返回: ["1:00","0:32"]

注意事项:

  • 输出的顺序没有要求。
  • 小时不会以零开头,比如 “01:00”?是不允许的,应为 “1:00”。
  • 分钟必须由两位数组成,可能会以零开头,比如 “10:2”?是无效的,应为 “10:02”。

 1 class Solution {
 2     func readBinaryWatch(_ num: Int) -> [String] {
 3         var times:[String] = [String]()   
 4         //轮询遍历
 5         for h in 0..<12
 6         {
 7             //将常量转换为变量
 8             var hour:Int = h
 9             for m in 0..<60
10             {
11                 //将常量转换为变量
12                 var minute:Int = m
13                 var number:Int = hour*64 + minute
14                 if number.bitCount() == num
15                 {
16                     //minute不足2位前面补0的写法:%02d
17                     var str = String(format: "%d:%02d",arguments:[hour,minute]) 
18                     times.append(str)
19                 }
20             }           
21         }
22         return times
23     }   
24 }
25 //Int扩展代码
26 extension Int
27 {
28     mutating func bitCount() -> Int
29     {
30         self = self - ((self >> 1) & 0x55555555);
31         self = (self & 0x33333333) + ((self >> 2) & 0x33333333);
32         self = (self + (self >> 4)) & 0x0f0f0f0f;
33         self = self + (self >> 8);
34         self = self + (self >> 16);
35         return self & 0x3f;        
36     }
37 }

8ms

 1 class Solution {
 2     func readBinaryWatch(_ num: Int) -> [String] {
 3        
 4         let leds = [1,2,4,8,1,16,32]
 5         var result = [String]()
 6         readBinaryWatch(leds,leds.count,0,(leds.count - num),&result) 
 7         return result
 8     } 
 9     
10     func readBinaryWatch(_ leds: [Int],_ ledsCount: Int,_ left: Int,_ right: Int,_ hours: Int,_ minutes: Int,_ result: inout [String]) {
11         if hours > 11 || minutes > 59 { return }
12         if right >= leds.count {
13             result.append(String(format: "%d:%02d",hours,minutes)) 
14             return 
15         }
16         
17         for index in left...right {
18             var currentHours = hours
19             var currentMinutes = minutes
20             
21             let value = leds[index]
22             if index >= 4 { currentMinutes += value } 
23             else { currentHours += value } 
24             
25             readBinaryWatch(leds,ledsCount,index + 1,right + 1,currentHours,currentMinutes,&result)  
26         }
27     }
28     
29 }

12ms

 1 class Solution {
 2     let hourArray = [
 3         [0], 4         [1,8], 5         [3,5,6,9,10], 6         [7,11]
 7     ]
 8     let minArray = [
 9         [0],10         [1,32],11         [3,10,12,17,18,20,24,33,34,36,40,48],12         [7,11,13,14,19,21,22,25,26,28,35,37,38,41,42,44,49,50,52,56],13         [15,23,27,29,30,39,43,45,46,51,53,54,57,58],14         [31,47,55,59]
15     ]
16     func readBinaryWatch(_ num: Int) -> [String] {
17         var result = [String]()
18         
19         for hNum in 0...min(3,num) {
20             let mNum = num - hNum
21             if mNum < minArray.count {
22                 for h in hourArray[hNum] {
23                     for m in minArray[mNum] {
24                         result.append("(h):(String(format: "%02d",m))")
25                     }
26                 }
27             }
28         }
29         return result
30     }
31 }

20ms

 1 class Solution {
 2     func readBinaryWatch(_ num: Int) -> [String] {
 3         var times: [String] = []
 4         let maxClock = num == 1 ? 8 : 11
 5         let maxMinute = num == 1 ? 32 : 59
 6         for clock in 0...maxClock {
 7             for minute in 0...maxMinute {
 8                 if bitCount(clock) + bitCount(minute) == num {
 9                     let time = String(format: "%d:%02d",clock,minute)
10                     times.append(time)
11                 }
12             }
13         }
14         
15         return times
16     }
17     
18     func bitCount(_ num: Int) -> Int {
19         var result = 0
20         var num2 = num
21         while num2 > 0 {
22             result += num2 & 1
23             num2 >>= 1
24         }
25         
26         return result
27     }
28 }

28ms

 1 class Solution {
 2     func readBinaryWatch(_ num: Int) -> [String] {
 3         var res = [String]()
 4 
 5         func bitCount(_ num: Int) -> Int {
 6             var count = 0
 7             var num = num
 8             while num > 0 {
 9                 count += num & 1
10                 num >>= 1
11             }
12             return count
13         }
14 
15         for h in 0...11 {
16             for m in 0...59 {
17                 if bitCount(h) + bitCount(m) == num {
18                     res.append(String(format: "%d:%02d",h,m))
19                 }
20             }
21         }
22 
23         return res
24     }
25 }

48ms

 1 class Solution {
 2     func readBinaryWatch(_ num: Int) -> [String] {
 3         var ret: [String] = []
 4         
 5         for i in 0...11 {
 6             for j in 0...59 {
 7                 let hourBinary = String(i,radix: 2).filter { $0 == "1" }
 8                 let minuteBinary = String(j,radix: 2).filter { $0 == "1" }
 9                 
10                 if hourBinary.count + minuteBinary.count == num {
11                     ret.append(String(format: "%d:%02d",i,j))
12                 }
13             }
14         }
15         
16         return ret
17     }
18 }

48ms

 1 class Solution {
 2     func readBinaryWatch(_ num: Int) -> [String] {
 3         var res = [String]()
 4 
 5         func findLEDs(_ h: Int,_ m: Int) -> Int {
 6             let hc = Array(String(h,radix: 2)).filter { $0 == "1" }.count
 7             let mc = Array(String(m,radix: 2)).filter { $0 == "1" }.count
 8             return hc + mc
 9         }
10 
11         for h in 0...11 {
12             for m in 0...59 {
13                 if findLEDs(h,m) == num {
14                     res.append(String(format: "%d:%02d",m))
15                 }
16             }
17         }
18 
19         return res
20     }
21 }

52ms

 1 class Solution {
 2     func readBinaryWatch(_ num: Int) -> [String] {
 3     var result = [String]()
 4     
 5     func findLEDs(_ h: Int,_ m: Int) -> Int {
 6         let hc = Array(String(h,radix: 2).characters).filter { $0 == "1" }.count
 7         let mc = Array(String(m,radix: 2).characters).filter { $0 == "1" }.count
 8         return hc + mc
 9     }
10     for h in 0...11 {
11         for m in 0...59 {
12             if findLEDs(h,m) == num {
13                 result.append(String(format: "%d:%02d",m))
14             }
15         }
16     }
17     return result
18     }
19 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读