Skip to content

Commit c9490c3

Browse files
committed
feat: add solutions to lc problem: No.1016
No.1016.Binary String With Substrings Representing 1 To N
1 parent c756389 commit c9490c3

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

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

+60-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040

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

43-
4(100)存在的话,2(10)一定存在。`n` 存在的话,`n >> 1` 也一定存在,所以只需要判断 `[n/2+1, n]` 范围的数字
43+
**方法一:数学**
44+
45+
4(100)存在的话,2(10)一定存在。`n` 存在的话,`n >> 1` 也一定存在,所以只需要判断 `[n/2+1, n]` 范围的数字。
4446

4547
<!-- tabs:start -->
4648

@@ -57,6 +59,12 @@ class Solution:
5759
return True
5860
```
5961

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))
66+
```
67+
6068
### **Java**
6169

6270
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -74,6 +82,19 @@ class Solution {
7482
}
7583
```
7684

85+
```java
86+
class Solution {
87+
public boolean queryString(String s, int n) {
88+
for (int i = 1; i <= n; ++i) {
89+
if (!s.contains(Integer.toBinaryString(i))) {
90+
return false;
91+
}
92+
}
93+
return true;
94+
}
95+
}
96+
```
97+
7798
### **Go**
7899

79100
```go
@@ -87,6 +108,17 @@ func queryString(s string, n int) bool {
87108
}
88109
```
89110

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+
90122
### **C++**
91123

92124
```cpp
@@ -103,6 +135,33 @@ public:
103135
};
104136
```
105137
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+
};
150+
```
151+
152+
### **TypeScript**
153+
154+
```ts
155+
function queryString(s: string, n: number): boolean {
156+
for (let i = 1; i <= n; ++i) {
157+
if (s.indexOf(i.toString(2)) === -1) {
158+
return false;
159+
}
160+
}
161+
return true;
162+
}
163+
```
164+
106165
### **...**
107166

108167
```

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

+57
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ class Solution:
4040
return True
4141
```
4242

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))
47+
```
48+
4349
### **Java**
4450

4551
```java
@@ -55,6 +61,19 @@ class Solution {
5561
}
5662
```
5763

64+
```java
65+
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))) {
69+
return false;
70+
}
71+
}
72+
return true;
73+
}
74+
}
75+
```
76+
5877
### **Go**
5978

6079
```go
@@ -68,6 +87,17 @@ func queryString(s string, n int) bool {
6887
}
6988
```
7089

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+
71101
### **C++**
72102

73103
```cpp
@@ -84,6 +114,33 @@ public:
84114
};
85115
```
86116
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+
131+
### **TypeScript**
132+
133+
```ts
134+
function queryString(s: string, n: number): boolean {
135+
for (let i = 1; i <= n; ++i) {
136+
if (s.indexOf(i.toString(2)) === -1) {
137+
return false;
138+
}
139+
}
140+
return true;
141+
}
142+
```
143+
87144
### **...**
88145

89146
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function queryString(s: string, n: number): boolean {
2+
for (let i = 1; i <= n; ++i) {
3+
if (s.indexOf(i.toString(2)) === -1) {
4+
return false;
5+
}
6+
}
7+
return true;
8+
}

0 commit comments

Comments
 (0)