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

[Swift]LeetCode401. 二进制手表 | Binary Watch,第1张

概述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 t

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”。

给定一个非负整数 代表当前 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..<6010             {11                 //将常量转换为变量12                 var minute:Int = m13                 var number:Int = hour*64 + minute14                 if number.bitCount() == num15                 {16                     //minute不足2位前面补0的写法:%02d17                     var str = String(format: "%d:%02d",arguments:[hour,minute]) 18                     times.append(str)19                 }20             }           21         }22         return times23     }   24 }25 //Int扩展代码26 extension Int27 {28     mutating func bitCount() -> Int29     {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 = hours19             var currentMinutes = minutes20             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 - hNum21             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 result30     }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 times16     }17     18     func bitCount(_ num: Int) -> Int {19         var result = 020         var num2 = num21         while num2 > 0 {22             result += num2 & 123             num2 >>= 124         }25         26         return result27     }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 & 110                 num >>= 111             }12             return count13         }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 res24     }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 ret17     }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 res20     }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 result18     }19 }
总结

以上是内存溢出为你收集整理的[Swift]LeetCode401. 二进制手表 | Binary Watch全部内容,希望文章能够帮你解决[Swift]LeetCode401. 二进制手表 | Binary Watch所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1023469.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存