Swift Solution with illustration


  • 0
    S
    func readBinaryWatch(_ num: Int) -> [String] {
            var res = [String]()
    
            func findLEDs(_ h: Int, _ m: Int) -> Int {
                let hc = Array(String(h, radix: 2).characters).filter { $0 == "1" }.count
                let mc = Array(String(m, radix: 2).characters).filter { $0 == "1" }.count
                return hc + mc
            }
    
            for h in 0...11 {
                for m in 0...59 {
                    if findLEDs(h, m) == num {
                        res.append(String(format: "%d:%02d", h, m))
                    }
                }
            }
    
            return res
        }
    


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.