Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了[Swift]LeetCode401. 二进制手表 | 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 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.

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

For example,the above binary watch reads "3:25".

Given a non-negative Integen 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,最低位在右侧。

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

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

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

案例:

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

注意事项:

  • 输出的顺序没有要求。
  • 小时不会以零开头,比如 “01:00” 是不允许的,应为 “1:00”。
  • 分钟必须由两位数组成,可能会以零开头,比如 “10:2” 是无效的,应为 “10:02”。
@H_675_83@ 1 class Solution {
@H_675_83@ 2     func readBinaryWatch(_ num: int) -> [String] {
@H_675_83@ 3         var times:[String] = [String]()   
@H_675_83@ 4         //轮询遍历
@H_675_83@ 5         for h in 0..<12
@H_675_83@ 6         {
@H_675_83@ 7             //将常量转换为变量
@H_675_83@ 8             var hour:Int = h
@H_675_83@ 9             for m in 0..<60
@H_675_83@10             {
@H_675_83@11                 //将常量转换为变量
@H_675_83@12                 var minute:Int = m
@H_675_83@13                 var number:Int = hour*64 + minute
@H_675_83@14                 if number.bitCount() == num
@H_675_83@15                 {
@H_675_83@16                     //minute不足2位前面补0的写法:%02d
@H_675_83@17                     var str = String(format: "%d:%02d",arguments:[hour,minute]) 
@H_675_83@18                     times.append(str)
@H_675_83@19                 }
@H_675_83@20             }           
@H_675_83@21         }
@H_675_83@22         return times
@H_675_83@23     }   
@H_675_83@24 }
@H_675_83@25 //Int扩展代码
@H_675_83@26 extension Int
@H_675_83@27 {
@H_675_83@28     mutaTing func bitCount() -> Int
@H_675_83@29     {
@H_675_83@30         self = self - ((self >> 1) & 0x55555555);
@H_675_83@31         self = (self & 0x33333333) + ((self >> 2) & 0x33333333);
@H_675_83@32         self = (self + (self >> 4)) & 0x0f0f0f0f;
@H_675_83@33         self = self + (self >> 8);
@H_675_83@34         self = self + (self >> 16);
@H_675_83@35         return self & 0x3f;        
@H_675_83@36     }
@H_675_83@37 }

8ms

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

12ms

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

20ms

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

28ms

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

48ms

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

48ms

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

52ms

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

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。