Skip to content

Commit 6fc4168

Browse files
committed
feat: add solutions to lc problem: No.1018
No.1018.Binary Prefix Divisible By 5
1 parent cd6fd0b commit 6fc4168

File tree

7 files changed

+51
-14
lines changed

7 files changed

+51
-14
lines changed

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
**方法一:脑筋急转弯**
4444

45-
我们注意到,字符串 $s$ 的长度不超过 $1000$,所以字符串 $s$ 能表示不超过 $100$ 个 $10$ 位的二进制整数,因此,我们可以粗略地估算,如果 $n \gt 1023$,那么 $s$ 肯定不能表示 $[1, n]$ 范围内的所有整数的二进制表示。
45+
我们注意到,字符串 $s$ 的长度不超过 $1000$,所以字符串 $s$ 能表示不超过 $1000$ 个 二进制整数,因此,如果 $n \gt 1000$,那么 $s$ 肯定不能表示 $[1,.. n]$ 范围内的所有整数的二进制表示。
4646

4747
另外,对于一个整数 $x$,如果 $x$ 的二进制表示是 $s$ 的子串,那么 $\lfloor x / 2 \rfloor$ 的二进制表示也是 $s$ 的子串。因此,我们只需要判断 $[\lfloor n / 2 \rfloor + 1,.. n]$ 范围内的整数的二进制表示是否是 $s$ 的子串即可。
4848

@@ -57,7 +57,7 @@
5757
```python
5858
class Solution:
5959
def queryString(self, s: str, n: int) -> bool:
60-
if n > 1023:
60+
if n > 1000:
6161
return False
6262
return all(bin(i)[2:] in s for i in range(n, n // 2, -1))
6363
```
@@ -69,7 +69,7 @@ class Solution:
6969
```java
7070
class Solution {
7171
public boolean queryString(String s, int n) {
72-
if (n > 1023) {
72+
if (n > 1000) {
7373
return false;
7474
}
7575
for (int i = n; i > n / 2; i--) {
@@ -88,7 +88,7 @@ class Solution {
8888
class Solution {
8989
public:
9090
bool queryString(string s, int n) {
91-
if (n > 1023) {
91+
if (n > 1000) {
9292
return false;
9393
}
9494
for (int i = n; i > n / 2; --i) {
@@ -107,7 +107,7 @@ public:
107107
108108
```go
109109
func queryString(s string, n int) bool {
110-
if n > 1023 {
110+
if n > 1000 {
111111
return false
112112
}
113113
for i := n; i > n/2; i-- {
@@ -123,7 +123,7 @@ func queryString(s string, n int) bool {
123123

124124
```ts
125125
function queryString(s: string, n: number): boolean {
126-
if (n > 1023) {
126+
if (n > 1000) {
127127
return false;
128128
}
129129
for (let i = n; i > n / 2; --i) {

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
```python
3535
class Solution:
3636
def queryString(self, s: str, n: int) -> bool:
37-
if n > 1023:
37+
if n > 1000:
3838
return False
3939
return all(bin(i)[2:] in s for i in range(n, n // 2, -1))
4040
```
@@ -44,7 +44,7 @@ class Solution:
4444
```java
4545
class Solution {
4646
public boolean queryString(String s, int n) {
47-
if (n > 1023) {
47+
if (n > 1000) {
4848
return false;
4949
}
5050
for (int i = n; i > n / 2; i--) {
@@ -63,7 +63,7 @@ class Solution {
6363
class Solution {
6464
public:
6565
bool queryString(string s, int n) {
66-
if (n > 1023) {
66+
if (n > 1000) {
6767
return false;
6868
}
6969
for (int i = n; i > n / 2; --i) {
@@ -82,7 +82,7 @@ public:
8282
8383
```go
8484
func queryString(s string, n int) bool {
85-
if n > 1023 {
85+
if n > 1000 {
8686
return false
8787
}
8888
for i := n; i > n/2; i-- {
@@ -98,7 +98,7 @@ func queryString(s string, n int) bool {
9898

9999
```ts
100100
function queryString(s: string, n: number): boolean {
101-
if (n > 1023) {
101+
if (n > 1000) {
102102
return false;
103103
}
104104
for (let i = n; i > n / 2; --i) {
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Solution:
22
def queryString(self, s: str, n: int) -> bool:
3-
if n > 1023:
3+
if n > 1000:
44
return False
55
return all(bin(i)[2:] in s for i in range(n, n // 2, -1))

solution/1000-1099/1018.Binary Prefix Divisible By 5/README.md

+14
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,20 @@ func prefixesDivBy5(nums []int) (ans []bool) {
118118
}
119119
```
120120

121+
### **TypeScript**
122+
123+
```ts
124+
function prefixesDivBy5(nums: number[]): boolean[] {
125+
const ans: boolean[] = [];
126+
let x = 0;
127+
for (const v of nums) {
128+
x = ((x << 1) | v) % 5;
129+
ans.push(x === 0);
130+
}
131+
return ans;
132+
}
133+
```
134+
121135
### **...**
122136

123137
```

solution/1000-1099/1018.Binary Prefix Divisible By 5/README_EN.md

+14
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,20 @@ func prefixesDivBy5(nums []int) (ans []bool) {
102102
}
103103
```
104104

105+
### **TypeScript**
106+
107+
```ts
108+
function prefixesDivBy5(nums: number[]): boolean[] {
109+
const ans: boolean[] = [];
110+
let x = 0;
111+
for (const v of nums) {
112+
x = ((x << 1) | v) % 5;
113+
ans.push(x === 0);
114+
}
115+
return ans;
116+
}
117+
```
118+
105119
### **...**
106120

107121
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function prefixesDivBy5(nums: number[]): boolean[] {
2+
const ans: boolean[] = [];
3+
let x = 0;
4+
for (const v of nums) {
5+
x = ((x << 1) | v) % 5;
6+
ans.push(x === 0);
7+
}
8+
return ans;
9+
}

solution/1000-1099/1021.Remove Outermost Parentheses/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@
6565

6666
**方法一:计数**
6767

68-
遍历字符串,遇到左括号 `(` 计数器加一,此时计数器不为 1 时,说明当前括号不是最外层括号,将其加入结果字符串。遇到右括号 `)` 计数器减一,此时计数器不为 0 时,说明当前括号不是最外层括号,将其加入结果字符串。
68+
遍历字符串,遇到左括号 `'('` 计数器加一,此时计数器不为 $1$ 时,说明当前括号不是最外层括号,将其加入结果字符串。遇到右括号 `')'` 计数器减一,此时计数器不为 $0$ 时,说明当前括号不是最外层括号,将其加入结果字符串。
6969

70-
时间复杂度 $O(n)$,忽略答案字符串的空间开销,空间复杂度 $O(1)$。其中 $n$ 为字符串长度
70+
时间复杂度 $O(n)$,其中 $n$ 为字符串长度。忽略答案字符串的空间开销,空间复杂度 $O(1)$。
7171

7272
<!-- tabs:start -->
7373

0 commit comments

Comments
 (0)