Skip to content

Commit dff85b9

Browse files
committed
Add solution and test-cases for problem 2460
1 parent f0a9eb1 commit dff85b9

File tree

3 files changed

+50
-23
lines changed

3 files changed

+50
-23
lines changed

leetcode/2401-2500/2460.Apply-Operations-to-an-Array/README.md

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
# [2460.Apply Operations to an Array][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
74

5+
You are given a **0-indexed** array `nums` of size `n` consisting of **non-negative** integers.
6+
7+
You need to apply `n - 1` operations to this array where, in the `ith` operation (**0-indexed**), you will apply the following on the `ith` element of `nums`:
8+
9+
- If `nums[i] == nums[i + 1]`, then multiply `nums[i]` by `2` and set `nums[i + 1]` to `0`. Otherwise, you skip this operation.
10+
11+
After performing **all** the operations, **shift** all the `0`'s to the **end** of the array.
12+
13+
- For example, the array `[1,0,2,0,0,1]` after shifting all its 0's to the end, is `[1,2,1,0,0,0]`.
14+
15+
Return the resulting array.
16+
17+
**Note** that the operations are applied **sequentially**, not all at once.
18+
819
**Example 1:**
920

1021
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
22+
Input: nums = [1,2,2,1,1,0]
23+
Output: [1,4,2,0,0,0]
24+
Explanation: We do the following operations:
25+
- i = 0: nums[0] and nums[1] are not equal, so we skip this operation.
26+
- i = 1: nums[1] and nums[2] are equal, we multiply nums[1] by 2 and change nums[2] to 0. The array becomes [1,4,0,1,1,0].
27+
- i = 2: nums[2] and nums[3] are not equal, so we skip this operation.
28+
- i = 3: nums[3] and nums[4] are equal, we multiply nums[3] by 2 and change nums[4] to 0. The array becomes [1,4,0,2,0,0].
29+
- i = 4: nums[4] and nums[5] are equal, we multiply nums[4] by 2 and change nums[5] to 0. The array becomes [1,4,0,2,0,0].
30+
After that, we shift the 0's to the end, which gives the array [1,4,2,0,0,0].
1331
```
1432

15-
## 题意
16-
> ...
17-
18-
## 题解
33+
**Example 2:**
1934

20-
### 思路1
21-
> ...
22-
Apply Operations to an Array
23-
```go
2435
```
25-
36+
Input: nums = [0,1]
37+
Output: [1,0]
38+
Explanation: No operation can be applied, we just shift the 0 to the end.
39+
```
2640

2741
## 结语
2842

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) []int {
4+
ans := make([]int, len(nums))
5+
for i := 0; i < len(nums)-1; i++ {
6+
if nums[i] == nums[i+1] {
7+
nums[i], nums[i+1] = nums[i]*2, 0
8+
continue
9+
}
10+
}
11+
index := 0
12+
for i := range nums {
13+
if nums[i] != 0 {
14+
ans[index] = nums[i]
15+
index++
16+
}
17+
}
18+
return ans
519
}

leetcode/2401-2500/2460.Apply-Operations-to-an-Array/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1, 2, 2, 1, 1, 0}, []int{1, 4, 2, 0, 0, 0}},
17+
{"TestCase2", []int{0, 1}, []int{1, 0}},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)