[Swift]LeetCode1016. 子串能表示从 1 到 N 数字的二进制串 | Binary String With Substrings Representing 1 To N

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(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. 1 <= S.length <= 1000
  2. 1 <= N <= 10^9

给定一个二进制字符串 S(一个仅由若干 '0' 和 '1' 构成的字符串)和一个正整数 N,若是对于从 1 到 N 的每一个整数 X,其二进制表示都是 S 的子串,就返回 true,不然返回 falsespa

示例 1:code

输入:S = "0110", N = 3
输出:true

示例 2:htm

输入:S = "0110", N = 4
输出:false

提示:blog

  1. 1 <= S.length <= 1000
  2. 1 <= N <= 10^9

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 }
相关文章
相关标签/搜索