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


    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址: https://www.cnblogs.com/strengthen/p/10588057.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.

    Example 1:

    Input: S = "0110", N = 3
    Output: true
    

    Example 2:

    Input: S = "0110", N = 4
    Output: false

    Note:

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

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

    示例 1:

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

    示例 2:

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

    提示:

    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 }
  • 相关阅读:
    3组Alpha冲刺5/6
    3组Beta冲刺4/5
    3组Alpha冲刺6/6
    3组Beta冲刺2/5
    3组Beta冲刺3/5
    delegate Demo (一个关于System.Timers.Timer的Demo)
    System.Web.HttpException 与 HTTP Error 404.13 Not Found问题解决说明
    常用SQL关于表的操作
    设计模式学习笔记:单例模式
    JS 实践杂记
  • 原文地址:https://www.cnblogs.com/strengthen/p/10588057.html
Copyright © 2020-2023  润新知