Skip to content

Commit 86a69f1

Browse files
committed
feat: add solutions to lc problem: No.1016
No.1016.Binary String With Substrings Representing 1 To N
1 parent 90fb0e0 commit 86a69f1

File tree

5 files changed

+46
-114
lines changed

5 files changed

+46
-114
lines changed

solution/1000-1099/1016.Binary String With Substrings Representing 1 To N/README.md

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@
4040

4141
<!-- 这里可写通用的实现逻辑 -->
4242

43-
**方法一:数学**
43+
**方法一:脑筋急转弯**
4444

45-
4(100)存在的话,2(10)一定存在。`n` 存在的话,`n >> 1` 也一定存在,所以只需要判断 `[n/2+1, n]` 范围的数字。
45+
我们注意到,字符串 $s$ 的长度不超过 $1000$,因此,字符串 $s$ 能表示不超过 $100$ 个 $10$ 位的二进制整数,因此,我们可以粗略地估算,如果 $n \gt 1023$,那么 $s$ 肯定不能表示 $[1, n]$ 范围内的所有整数的二进制表示。
46+
47+
另外,对于一个整数 $x$,如果 $x$ 的二进制表示是 $s$ 的子串,那么 $\lfloor x / 2 \rfloor$ 的二进制表示也是 $s$ 的子串。因此,我们只需要判断 $[\lfloor n / 2 \rfloor + 1,.. n]$ 范围内的整数的二进制表示是否是 $s$ 的子串即可。
48+
49+
时间复杂度 $O(m^2),空间复杂度 $O(m)$,其中 $m$ 是字符串 $s$ 的长度。
4650

4751
<!-- tabs:start -->
4852

@@ -53,16 +57,9 @@
5357
```python
5458
class Solution:
5559
def queryString(self, s: str, n: int) -> bool:
56-
for i in range(n, n // 2, -1):
57-
if bin(i)[2:] not in s:
58-
return False
59-
return True
60-
```
61-
62-
```python
63-
class Solution:
64-
def queryString(self, s: str, n: int) -> bool:
65-
return all(bin(i)[2:] in s for i in range(1, n + 1))
60+
if n > 1023:
61+
return False
62+
return all(bin(i)[2:] in s for i in range(n, n // 2, -1))
6663
```
6764

6865
### **Java**
@@ -95,30 +92,6 @@ class Solution {
9592
}
9693
```
9794

98-
### **Go**
99-
100-
```go
101-
func queryString(s string, n int) bool {
102-
for i := n; i > n/2; i-- {
103-
if !strings.Contains(s, strconv.FormatInt(int64(i), 2)) {
104-
return false
105-
}
106-
}
107-
return true
108-
}
109-
```
110-
111-
```go
112-
func queryString(s string, n int) bool {
113-
for i := 1; i <= n; i++ {
114-
if !strings.Contains(s, strconv.FormatInt(int64(i), 2)) {
115-
return false
116-
}
117-
}
118-
return true
119-
}
120-
```
121-
12295
### **C++**
12396

12497
```cpp
@@ -128,32 +101,33 @@ public:
128101
for (int i = n; i > n / 2; --i) {
129102
string b = bitset<32>(i).to_string();
130103
b = b.substr(b.find_first_not_of('0'));
131-
if (s.find(b) == string::npos) return false;
104+
if (s.find(b) == string::npos) {
105+
return false;
106+
}
132107
}
133108
return true;
134109
}
135110
};
136111
```
137112
138-
```cpp
139-
class Solution {
140-
public:
141-
bool queryString(string s, int n) {
142-
for (int i = 1; i <= n; ++i) {
143-
string b = bitset<32>(i).to_string();
144-
b = b.substr(b.find_first_not_of('0'));
145-
if (s.find(b) == string::npos) return false;
146-
}
147-
return true;
148-
}
149-
};
113+
### **Go**
114+
115+
```go
116+
func queryString(s string, n int) bool {
117+
for i := n; i > n/2; i-- {
118+
if !strings.Contains(s, strconv.FormatInt(int64(i), 2)) {
119+
return false
120+
}
121+
}
122+
return true
123+
}
150124
```
151125

152126
### **TypeScript**
153127

154128
```ts
155129
function queryString(s: string, n: number): boolean {
156-
for (let i = 1; i <= n; ++i) {
130+
for (let i = n; i > n / 2; --i) {
157131
if (s.indexOf(i.toString(2)) === -1) {
158132
return false;
159133
}

solution/1000-1099/1016.Binary String With Substrings Representing 1 To N/README_EN.md

Lines changed: 14 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,9 @@
3434
```python
3535
class Solution:
3636
def queryString(self, s: str, n: int) -> bool:
37-
for i in range(n, n // 2, -1):
38-
if bin(i)[2:] not in s:
39-
return False
40-
return True
41-
```
42-
43-
```python
44-
class Solution:
45-
def queryString(self, s: str, n: int) -> bool:
46-
return all(bin(i)[2:] in s for i in range(1, n + 1))
37+
if n > 1023:
38+
return False
39+
return all(bin(i)[2:] in s for i in range(n, n // 2, -1))
4740
```
4841

4942
### **Java**
@@ -61,17 +54,22 @@ class Solution {
6154
}
6255
```
6356

64-
```java
57+
### **C++**
58+
59+
```cpp
6560
class Solution {
66-
public boolean queryString(String s, int n) {
67-
for (int i = 1; i <= n; ++i) {
68-
if (!s.contains(Integer.toBinaryString(i))) {
61+
public:
62+
bool queryString(string s, int n) {
63+
for (int i = n; i > n / 2; --i) {
64+
string b = bitset<32>(i).to_string();
65+
b = b.substr(b.find_first_not_of('0'));
66+
if (s.find(b) == string::npos) {
6967
return false;
7068
}
7169
}
7270
return true;
7371
}
74-
}
72+
};
7573
```
7674
7775
### **Go**
@@ -87,52 +85,11 @@ func queryString(s string, n int) bool {
8785
}
8886
```
8987

90-
```go
91-
func queryString(s string, n int) bool {
92-
for i := 1; i <= n; i++ {
93-
if !strings.Contains(s, strconv.FormatInt(int64(i), 2)) {
94-
return false
95-
}
96-
}
97-
return true
98-
}
99-
```
100-
101-
### **C++**
102-
103-
```cpp
104-
class Solution {
105-
public:
106-
bool queryString(string s, int n) {
107-
for (int i = n; i > n / 2; --i) {
108-
string b = bitset<32>(i).to_string();
109-
b = b.substr(b.find_first_not_of('0'));
110-
if (s.find(b) == string::npos) return false;
111-
}
112-
return true;
113-
}
114-
};
115-
```
116-
117-
```cpp
118-
class Solution {
119-
public:
120-
bool queryString(string s, int n) {
121-
for (int i = 1; i <= n; ++i) {
122-
string b = bitset<32>(i).to_string();
123-
b = b.substr(b.find_first_not_of('0'));
124-
if (s.find(b) == string::npos) return false;
125-
}
126-
return true;
127-
}
128-
};
129-
```
130-
13188
### **TypeScript**
13289

13390
```ts
13491
function queryString(s: string, n: number): boolean {
135-
for (let i = 1; i <= n; ++i) {
92+
for (let i = n; i > n / 2; --i) {
13693
if (s.indexOf(i.toString(2)) === -1) {
13794
return false;
13895
}

solution/1000-1099/1016.Binary String With Substrings Representing 1 To N/Solution.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ class Solution {
44
for (int i = n; i > n / 2; --i) {
55
string b = bitset<32>(i).to_string();
66
b = b.substr(b.find_first_not_of('0'));
7-
if (s.find(b) == string::npos) return false;
7+
if (s.find(b) == string::npos) {
8+
return false;
9+
}
810
}
911
return true;
1012
}
11-
};
13+
};
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
class Solution:
22
def queryString(self, s: str, n: int) -> bool:
3-
for i in range(n, n // 2, -1):
4-
if bin(i)[2:] not in s:
5-
return False
6-
return True
3+
if n > 1023:
4+
return False
5+
return all(bin(i)[2:] in s for i in range(n, n // 2, -1))

solution/1000-1099/1016.Binary String With Substrings Representing 1 To N/Solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function queryString(s: string, n: number): boolean {
2-
for (let i = 1; i <= n; ++i) {
2+
for (let i = n; i > n / 2; --i) {
33
if (s.indexOf(i.toString(2)) === -1) {
44
return false;
55
}

0 commit comments

Comments
 (0)