★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-zqwfrcvh-md.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given a binary string S
(a string consisting only of '0' and '1's) and a positive integer N
, return true if and only if for every integer X from 1 to N, the binary representation of X is a substring of S.git
Example 1:github
Input: S = "0110", N = 3 Output: true
Example 2:微信
Input: S = "0110", N = 4 Output: false
Note:app
1 <= S.length <= 1000
1 <= N <= 10^9
給定一個二進制字符串 S
(一個僅由若干 '0' 和 '1' 構成的字符串)和一個正整數 N
,若是對於從 1
到 N
的每一個整數 X
,其二進制表示都是 S
的子串,就返回 true
,不然返回 false
。spa
示例 1:code
輸入:S = "0110", N = 3 輸出:true
示例 2:htm
輸入:S = "0110", N = 4 輸出:false
提示:blog
1 <= S.length <= 1000
1 <= N <= 10^9
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 }