Skip to content

Commit 01d4e46

Browse files
committed
feat: add solutions to lc problem: No.1018
No.1018.Binary Prefix Divisible By 5
1 parent 60bc1d9 commit 01d4e46

File tree

8 files changed

+158
-3
lines changed

8 files changed

+158
-3
lines changed

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

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,75 @@
4747

4848
<!-- 这里可写通用的实现逻辑 -->
4949

50+
**方法一:模拟**
51+
52+
遍历数组,每一次遍历都将当前数字加到前面的数字上,然后对 $5$ 取模,如果结果为 $0$,则当前数字可以被 $5$ 整除,答案设置为 `true`,否则为 `false`
53+
54+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度。
55+
5056
<!-- tabs:start -->
5157

5258
### **Python3**
5359

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

5662
```python
57-
63+
class Solution:
64+
def prefixesDivBy5(self, nums: List[int]) -> List[bool]:
65+
ans = []
66+
x = 0
67+
for v in nums:
68+
x = (x << 1 | v) % 5
69+
ans.append(x == 0)
70+
return ans
5871
```
5972

6073
### **Java**
6174

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

6477
```java
78+
class Solution {
79+
public List<Boolean> prefixesDivBy5(int[] nums) {
80+
List<Boolean> ans = new ArrayList<>();
81+
int x = 0;
82+
for (int v : nums) {
83+
x = (x << 1 | v) % 5;
84+
ans.add(x == 0);
85+
}
86+
return ans;
87+
}
88+
}
89+
```
90+
91+
### **C++**
92+
93+
```cpp
94+
class Solution {
95+
public:
96+
vector<bool> prefixesDivBy5(vector<int>& nums) {
97+
vector<bool> ans;
98+
int x = 0;
99+
for (int v : nums) {
100+
x = (x << 1 | v) % 5;
101+
ans.push_back(x == 0);
102+
}
103+
return ans;
104+
}
105+
};
106+
```
65107
108+
### **Go**
109+
110+
```go
111+
func prefixesDivBy5(nums []int) (ans []bool) {
112+
x := 0
113+
for _, v := range nums {
114+
x = (x<<1 | v) % 5
115+
ans = append(ans, x == 0)
116+
}
117+
return
118+
}
66119
```
67120

68121
### **...**

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

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,60 @@ Only the first number is divisible by 5, so answer[0] is true.
4646
### **Python3**
4747

4848
```python
49-
49+
class Solution:
50+
def prefixesDivBy5(self, nums: List[int]) -> List[bool]:
51+
ans = []
52+
x = 0
53+
for v in nums:
54+
x = (x << 1 | v) % 5
55+
ans.append(x == 0)
56+
return ans
5057
```
5158

5259
### **Java**
5360

5461
```java
62+
class Solution {
63+
public List<Boolean> prefixesDivBy5(int[] nums) {
64+
List<Boolean> ans = new ArrayList<>();
65+
int x = 0;
66+
for (int v : nums) {
67+
x = (x << 1 | v) % 5;
68+
ans.add(x == 0);
69+
}
70+
return ans;
71+
}
72+
}
73+
```
74+
75+
### **C++**
76+
77+
```cpp
78+
class Solution {
79+
public:
80+
vector<bool> prefixesDivBy5(vector<int>& nums) {
81+
vector<bool> ans;
82+
int x = 0;
83+
for (int v : nums) {
84+
x = (x << 1 | v) % 5;
85+
ans.push_back(x == 0);
86+
}
87+
return ans;
88+
}
89+
};
90+
```
5591
92+
### **Go**
93+
94+
```go
95+
func prefixesDivBy5(nums []int) (ans []bool) {
96+
x := 0
97+
for _, v := range nums {
98+
x = (x<<1 | v) % 5
99+
ans = append(ans, x == 0)
100+
}
101+
return
102+
}
56103
```
57104

58105
### **...**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
vector<bool> prefixesDivBy5(vector<int>& nums) {
4+
vector<bool> ans;
5+
int x = 0;
6+
for (int v : nums) {
7+
x = (x << 1 | v) % 5;
8+
ans.push_back(x == 0);
9+
}
10+
return ans;
11+
}
12+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func prefixesDivBy5(nums []int) (ans []bool) {
2+
x := 0
3+
for _, v := range nums {
4+
x = (x<<1 | v) % 5
5+
ans = append(ans, x == 0)
6+
}
7+
return
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public List<Boolean> prefixesDivBy5(int[] nums) {
3+
List<Boolean> ans = new ArrayList<>();
4+
int x = 0;
5+
for (int v : nums) {
6+
x = (x << 1 | v) % 5;
7+
ans.add(x == 0);
8+
}
9+
return ans;
10+
}
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def prefixesDivBy5(self, nums: List[int]) -> List[bool]:
3+
ans = []
4+
x = 0
5+
for v in nums:
6+
x = (x << 1 | v) % 5
7+
ans.append(x == 0)
8+
return ans

solution/CONTEST_README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@
2424

2525
## 往期竞赛
2626

27+
#### 第 332 场周赛(2023-02-12 10:30, 90 分钟) 参赛人数 4547
28+
29+
- [2562. 找出数组的串联值](/solution/2500-2599/2562.Find%20the%20Array%20Concatenation%20Value/README.md)
30+
- [2563. 统计公平数对的数目](/solution/2500-2599/2563.Count%20the%20Number%20of%20Fair%20Pairs/README.md)
31+
- [2564. 子字符串异或查询](/solution/2500-2599/2564.Substring%20XOR%20Queries/README.md)
32+
- [2565. 最少得分子序列](/solution/2500-2599/2565.Subsequence%20With%20the%20Minimum%20Score/README.md)
33+
34+
2735
#### 第 331 场周赛(2023-02-05 10:30, 90 分钟) 参赛人数 4256
2836

2937
- [2558. 从数量最多的堆取走礼物](/solution/2500-2599/2558.Take%20Gifts%20From%20the%20Richest%20Pile/README.md)
@@ -2584,7 +2592,7 @@
25842592
- [936. 戳印序列](/solution/0900-0999/0936.Stamping%20The%20Sequence/README.md)
25852593

25862594

2587-
#### 第 108 场周赛(2018-10-28 09:30, 90 分钟) 参赛人数 523
2595+
#### 第 108 场周赛(2018-10-28 09:30, 90 分钟) 参赛人数 524
25882596

25892597
- [929. 独特的电子邮件地址](/solution/0900-0999/0929.Unique%20Email%20Addresses/README.md)
25902598
- [930. 和相同的二元子数组](/solution/0900-0999/0930.Binary%20Subarrays%20With%20Sum/README.md)

solution/CONTEST_README_EN.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ Get your rating changes right after the completion of LeetCode contests, https:/
2525

2626
## Past Contests
2727

28+
#### Weekly Contest 332
29+
30+
- [2562. Find the Array Concatenation Value](/solution/2500-2599/2562.Find%20the%20Array%20Concatenation%20Value/README_EN.md)
31+
- [2563. Count the Number of Fair Pairs](/solution/2500-2599/2563.Count%20the%20Number%20of%20Fair%20Pairs/README_EN.md)
32+
- [2564. Substring XOR Queries](/solution/2500-2599/2564.Substring%20XOR%20Queries/README_EN.md)
33+
- [2565. Subsequence With the Minimum Score](/solution/2500-2599/2565.Subsequence%20With%20the%20Minimum%20Score/README_EN.md)
34+
35+
2836
#### Weekly Contest 331
2937

3038
- [2558. Take Gifts From the Richest Pile](/solution/2500-2599/2558.Take%20Gifts%20From%20the%20Richest%20Pile/README_EN.md)

0 commit comments

Comments
 (0)