Skip to content

Commit e3ba2d3

Browse files
committed
Add solution 1018
1 parent 4067a78 commit e3ba2d3

File tree

6 files changed

+470
-262
lines changed

6 files changed

+470
-262
lines changed

README.md

+262-262
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package leetcode
2+
3+
func prefixesDivBy5(a []int) []bool {
4+
res, num := make([]bool, len(a)), 0
5+
for i, v := range a {
6+
num = (num<<1 | v) % 5
7+
res[i] = num == 0
8+
}
9+
return res
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1018 struct {
9+
para1018
10+
ans1018
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1018 struct {
16+
a []int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans1018 struct {
22+
one []bool
23+
}
24+
25+
func Test_Problem1018(t *testing.T) {
26+
27+
qs := []question1018{
28+
29+
{
30+
para1018{[]int{0, 1, 1}},
31+
ans1018{[]bool{true, false, false}},
32+
},
33+
34+
{
35+
para1018{[]int{1, 1, 1}},
36+
ans1018{[]bool{false, false, false}},
37+
},
38+
39+
{
40+
para1018{[]int{0, 1, 1, 1, 1, 1}},
41+
ans1018{[]bool{true, false, false, false, true, false}},
42+
},
43+
44+
{
45+
para1018{[]int{1, 1, 1, 0, 1}},
46+
ans1018{[]bool{false, false, false, false, false}},
47+
},
48+
}
49+
50+
fmt.Printf("------------------------Leetcode Problem 1018------------------------\n")
51+
52+
for _, q := range qs {
53+
_, p := q.ans1018, q.para1018
54+
fmt.Printf("【input】:%v 【output】:%v\n", p, prefixesDivBy5(p.a))
55+
}
56+
fmt.Printf("\n\n\n")
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# [1018. Binary Prefix Divisible By 5](https://leetcode.com/problems/binary-prefix-divisible-by-5/)
2+
3+
4+
## 题目
5+
6+
Given an array `A` of `0`s and `1`s, consider `N_i`: the i-th subarray from `A[0]` to `A[i]` interpreted as a binary number (from most-significant-bit to least-significant-bit.)
7+
8+
Return a list of booleans `answer`, where `answer[i]` is `true` if and only if `N_i` is divisible by 5.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: [0,1,1]
14+
Output: [true,false,false]
15+
Explanation:
16+
The input numbers in binary are 0, 01, 011; which are 0, 1, and 3 in base-10. Only the first number is divisible by 5, so answer[0] is true.
17+
18+
```
19+
20+
**Example 2:**
21+
22+
```
23+
Input: [1,1,1]
24+
Output: [false,false,false]
25+
26+
```
27+
28+
**Example 3:**
29+
30+
```
31+
Input: [0,1,1,1,1,1]
32+
Output: [true,false,false,false,true,false]
33+
34+
```
35+
36+
**Example 4:**
37+
38+
```
39+
Input: [1,1,1,0,1]
40+
Output: [false,false,false,false,false]
41+
42+
```
43+
44+
**Note:**
45+
46+
1. `1 <= A.length <= 30000`
47+
2. `A[i]` is `0` or `1`
48+
49+
## 题目大意
50+
51+
给定由若干 0 和 1 组成的数组 A。我们定义 N_i:从 A[0] 到 A[i] 的第 i 个子数组被解释为一个二进制数(从最高有效位到最低有效位)。返回布尔值列表 answer,只有当 N_i 可以被 5 整除时,答案 answer[i] 为 true,否则为 false。
52+
53+
## 解题思路
54+
55+
- 简单题。每扫描数组中的一个数字,累计转换成二进制数对 5 取余,如果余数为 0,则存入 true,否则存入 false。
56+
57+
## 代码
58+
59+
```go
60+
package leetcode
61+
62+
func prefixesDivBy5(a []int) []bool {
63+
res, num := make([]bool, len(a)), 0
64+
for i, v := range a {
65+
num = (num<<1 | v) % 5
66+
res[i] = num == 0
67+
}
68+
return res
69+
}
70+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# [1018. Binary Prefix Divisible By 5](https://leetcode.com/problems/binary-prefix-divisible-by-5/)
2+
3+
4+
## 题目
5+
6+
Given an array `A` of `0`s and `1`s, consider `N_i`: the i-th subarray from `A[0]` to `A[i]` interpreted as a binary number (from most-significant-bit to least-significant-bit.)
7+
8+
Return a list of booleans `answer`, where `answer[i]` is `true` if and only if `N_i` is divisible by 5.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: [0,1,1]
14+
Output: [true,false,false]
15+
Explanation:
16+
The input numbers in binary are 0, 01, 011; which are 0, 1, and 3 in base-10. Only the first number is divisible by 5, so answer[0] is true.
17+
18+
```
19+
20+
**Example 2:**
21+
22+
```
23+
Input: [1,1,1]
24+
Output: [false,false,false]
25+
26+
```
27+
28+
**Example 3:**
29+
30+
```
31+
Input: [0,1,1,1,1,1]
32+
Output: [true,false,false,false,true,false]
33+
34+
```
35+
36+
**Example 4:**
37+
38+
```
39+
Input: [1,1,1,0,1]
40+
Output: [false,false,false,false,false]
41+
42+
```
43+
44+
**Note:**
45+
46+
1. `1 <= A.length <= 30000`
47+
2. `A[i]` is `0` or `1`
48+
49+
## 题目大意
50+
51+
给定由若干 0 和 1 组成的数组 A。我们定义 N_i:从 A[0] 到 A[i] 的第 i 个子数组被解释为一个二进制数(从最高有效位到最低有效位)。返回布尔值列表 answer,只有当 N_i 可以被 5 整除时,答案 answer[i] 为 true,否则为 false。
52+
53+
## 解题思路
54+
55+
- 简单题。每扫描数组中的一个数字,累计转换成二进制数对 5 取余,如果余数为 0,则存入 true,否则存入 false。
56+
57+
## 代码
58+
59+
```go
60+
package leetcode
61+
62+
func prefixesDivBy5(a []int) []bool {
63+
res, num := make([]bool, len(a)), 0
64+
for i, v := range a {
65+
num = (num<<1 | v) % 5
66+
res[i] = num == 0
67+
}
68+
return res
69+
}
70+
```

website/content/menu/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ headless: true
483483
- [1005.Maximize-Sum-Of-Array-After-K-Negations]({{< relref "/ChapterFour/1005.Maximize-Sum-Of-Array-After-K-Negations.md" >}})
484484
- [1011.Capacity-To-Ship-Packages-Within-D-Days]({{< relref "/ChapterFour/1011.Capacity-To-Ship-Packages-Within-D-Days.md" >}})
485485
- [1017.Convert-to-Base--2]({{< relref "/ChapterFour/1017.Convert-to-Base--2.md" >}})
486+
- [1018.Binary-Prefix-Divisible-By-5]({{< relref "/ChapterFour/1018.Binary-Prefix-Divisible-By-5.md" >}})
486487
- [1019.Next-Greater-Node-In-Linked-List]({{< relref "/ChapterFour/1019.Next-Greater-Node-In-Linked-List.md" >}})
487488
- [1020.Number-of-Enclaves]({{< relref "/ChapterFour/1020.Number-of-Enclaves.md" >}})
488489
- [1021.Remove-Outermost-Parentheses]({{< relref "/ChapterFour/1021.Remove-Outermost-Parentheses.md" >}})

0 commit comments

Comments
 (0)