在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given a binary string Example 1: Input: S = 3
Output: true
Example 2: Input: S = 4
Output: false
Note:
给定一个二进制字符串 示例 1: 输入:S = "0110", N = 3 输出:true 示例 2: 输入:S = "0110", N = 4 输出:false 提示:
Runtime: 8 ms
Memory Usage: 20.4 MB
1 class Solution { 2 func queryString(_ S: String, _ N: Int) -> Bool { 3 if N > 2400 {return false} 4 for i in 1...N 5 { 6 var str:String = String() 7 var x:Int = i 8 while (x != 0) 9 { 10 str.append((x % 2 + 48).ASCII) 11 x /= 2 12 } 13 str = String(str.reversed()) 14 if !S.contains(str) {return false} 15 } 16 return true 17 } 18 } 19 20 //Int扩展 21 extension Int 22 { 23 //Int转Character,ASCII值(定义大写为字符值) 24 var ASCII:Character 25 { 26 get {return Character(UnicodeScalar(self)!)} 27 } 28 }
|
请发表评论