|
1 | 1 | # [2460.Apply Operations to an Array][title] |
2 | 2 |
|
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 | | -
|
6 | 3 | ## Description |
7 | 4 |
|
| 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 | + |
8 | 19 | **Example 1:** |
9 | 20 |
|
10 | 21 | ``` |
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]. |
13 | 31 | ``` |
14 | 32 |
|
15 | | -## 题意 |
16 | | -> ... |
17 | | -
|
18 | | -## 题解 |
| 33 | +**Example 2:** |
19 | 34 |
|
20 | | -### 思路1 |
21 | | -> ... |
22 | | -Apply Operations to an Array |
23 | | -```go |
24 | 35 | ``` |
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 | +``` |
26 | 40 |
|
27 | 41 | ## 结语 |
28 | 42 |
|
|
0 commit comments