原文链接: https://leetcode-cn.com/problems/binary-string-with-substrings-representing-1-to-n
英文原文
Given a binary string s and a positive integer n, return true if the binary representation of all the integers in the range [1, n] are substrings of s, or false otherwise.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "0110", n = 3 Output: true
Example 2:
Input: s = "0110", n = 4 Output: false
Constraints:
1 <= s.length <= 1000s[i]is either'0'or'1'.1 <= n <= 109
中文题目
给定一个二进制字符串 S(一个仅由若干 '0' 和 '1' 构成的字符串)和一个正整数 N,如果对于从 1 到 N 的每个整数 X,其二进制表示都是 S 的子串,就返回 true,否则返回 false。
示例 1:
输入:S = "0110", N = 3 输出:true
示例 2:
输入:S = "0110", N = 4 输出:false
提示:
1 <= S.length <= 10001 <= N <= 10^9
通过代码
高赞题解
class Solution {
public boolean queryString(String S, int N) {
for(int i=1;i<=N;i++) {
if(!S.contains(Integer.toBinaryString(i)))
return false;
}
return true;
}
}
统计信息
| 通过次数 | 提交次数 | AC比率 |
|---|---|---|
| 5425 | 9286 | 58.4% |
提交历史
| 提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
|---|