Skip to content

Commit e094d69

Browse files
committedMar 24, 2022
feat: add solutions to lc problem: No.1016
No.1016.Binary String With Substrings Representing 1 To N
1 parent 8d66658 commit e094d69

File tree

6 files changed

+125
-2
lines changed

6 files changed

+125
-2
lines changed
 

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

+46-1
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,67 @@
4040

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

43+
4(100)存在的话,2(10)一定存在。`n` 存在的话,`n >> 1` 也一定存在,所以只需要判断 `[n/2+1, n]` 范围的数字
44+
4345
<!-- tabs:start -->
4446

4547
### **Python3**
4648

4749
<!-- 这里可写当前语言的特殊实现逻辑 -->
4850

4951
```python
50-
52+
class Solution:
53+
def queryString(self, s: str, n: int) -> bool:
54+
for i in range(n, n // 2, -1):
55+
if bin(i)[2:] not in s:
56+
return False
57+
return True
5158
```
5259

5360
### **Java**
5461

5562
<!-- 这里可写当前语言的特殊实现逻辑 -->
5663

5764
```java
65+
class Solution {
66+
public boolean queryString(String s, int n) {
67+
for (int i = n; i > n / 2; i--) {
68+
if (!s.contains(Integer.toBinaryString(i))) {
69+
return false;
70+
}
71+
}
72+
return true;
73+
}
74+
}
75+
```
76+
77+
### **Go**
78+
79+
```go
80+
func queryString(s string, n int) bool {
81+
for i := n; i > n/2; i-- {
82+
if !strings.Contains(s, strconv.FormatInt(int64(i), 2)) {
83+
return false
84+
}
85+
}
86+
return true
87+
}
88+
```
5889

90+
### **C++**
91+
92+
```cpp
93+
class Solution {
94+
public:
95+
bool queryString(string s, int n) {
96+
for (int i = n; i > n / 2; --i) {
97+
string b = bitset<32>(i).to_string();
98+
b = b.substr(b.find_first_not_of('0'));
99+
if (s.find(b) == string::npos) return false;
100+
}
101+
return true;
102+
}
103+
};
59104
```
60105
61106
### **...**

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

+44-1
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,56 @@
3232
### **Python3**
3333

3434
```python
35-
35+
class Solution:
36+
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
3641
```
3742

3843
### **Java**
3944

4045
```java
46+
class Solution {
47+
public boolean queryString(String s, int n) {
48+
for (int i = n; i > n / 2; i--) {
49+
if (!s.contains(Integer.toBinaryString(i))) {
50+
return false;
51+
}
52+
}
53+
return true;
54+
}
55+
}
56+
```
57+
58+
### **Go**
59+
60+
```go
61+
func queryString(s string, n int) bool {
62+
for i := n; i > n/2; i-- {
63+
if !strings.Contains(s, strconv.FormatInt(int64(i), 2)) {
64+
return false
65+
}
66+
}
67+
return true
68+
}
69+
```
4170

71+
### **C++**
72+
73+
```cpp
74+
class Solution {
75+
public:
76+
bool queryString(string s, int n) {
77+
for (int i = n; i > n / 2; --i) {
78+
string b = bitset<32>(i).to_string();
79+
b = b.substr(b.find_first_not_of('0'));
80+
if (s.find(b) == string::npos) return false;
81+
}
82+
return true;
83+
}
84+
};
4285
```
4386
4487
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
bool queryString(string s, int n) {
4+
for (int i = n; i > n / 2; --i) {
5+
string b = bitset<32>(i).to_string();
6+
b = b.substr(b.find_first_not_of('0'));
7+
if (s.find(b) == string::npos) return false;
8+
}
9+
return true;
10+
}
11+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func queryString(s string, n int) bool {
2+
for i := n; i > n/2; i-- {
3+
if !strings.Contains(s, strconv.FormatInt(int64(i), 2)) {
4+
return false
5+
}
6+
}
7+
return true
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public boolean queryString(String s, int n) {
3+
for (int i = n; i > n / 2; i--) {
4+
if (!s.contains(Integer.toBinaryString(i))) {
5+
return false;
6+
}
7+
}
8+
return true;
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
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

0 commit comments

Comments
 (0)
Please sign in to comment.