8000 :sparkles: Added solution to 1018 · reverse/awesome-golang-leetcode@6a12c94 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6a12c94

Browse files
author
Sathish Babu
committed
✨ Added solution to 1018
1 parent 59518b8 commit 6a12c94

File tree

3 files changed

+56
-9
lines changed

3 files changed

+56
-9
lines changed

src/1018.Binary-Prefix-Divisible-By-5/README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,38 @@
55
66
## Description
77

8+
Given an array A of 0s and 1s, 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.)
9+
10+
Return a list of booleans answer, where answer[i] is true if and only if N_i is divisible by 5.
11+
812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: [0,1,1]
16+
Output: [true,false,false]
17+
Explanation:
18+
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.
19+
```
20+
21+
**Example 2:**
22+
23+
```
24+
Input: [1,1,1]
25+
Output: [false,false,false]
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+
**Example 4:**
36+
37+
```
38+
Input: [1,1,1,0,1]
39+
Output: [false,false,false,false,false]
1340
```
1441

1542
## 题意
Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(A []int) []bool {
4+
n := len(A)
5+
if n == 1 {
6+
if A[0] == 0 {
7+
return []bool{true}
8+
} else {
9+
return []bool{false}
10+
}
11+
}
12+
ans, v := make([]bool, n), 0
13+
for i := 0; i < n; i++ {
14+
v = (2*v + A[i]) % 5
15+
if v == 0 {
16+
ans[i] = true
17+
} else {
18+
ans[i] = false
19+
}
20+
}
21+
return ans
522
}

src/1018.Binary-Prefix-Divisible-By-5/Solution_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect []bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase", []int{0}, []bool{true}},
17+
{"TestCase", []int{1}, []bool{false}},
18+
{"TestCase", []int{0, 1, 1}, []bool{true, false, false}},
19+
{"TestCase", []int{1, 1, 1}, []bool{false, false, false}},
20+
{"TestCase", []int{1, 1, 1, 0, 1}, []bool{false, false, false, false, false}},
21+
{"TestCase", []int{0, 1, 1, 1, 1, 1}, []bool{true, false, false, false, true, false}},
1922
}
2023

2124
// 开始测试

0 commit comments

Comments
 (0)
0