|
| 1 | +<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of size <code>n</code> consisting of <strong>non-negative</strong> integers.</p> |
| 2 | + |
| 3 | +<p>You need to apply <code>n - 1</code> operations to this array where, in the <code>i<sup>th</sup></code> operation (<strong>0-indexed</strong>), you will apply the following on the <code>i<sup>th</sup></code> element of <code>nums</code>:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>If <code>nums[i] == nums[i + 1]</code>, then multiply <code>nums[i]</code> by <code>2</code> and set <code>nums[i + 1]</code> to <code>0</code>. Otherwise, you skip this operation.</li> |
| 7 | +</ul> |
| 8 | + |
| 9 | +<p>After performing <strong>all</strong> the operations, <strong>shift</strong> all the <code>0</code>'s to the <strong>end</strong> of the array.</p> |
| 10 | + |
| 11 | +<ul> |
| 12 | + <li>For example, the array <code>[1,0,2,0,0,1]</code> after shifting all its <code>0</code>'s to the end, is <code>[1,2,1,0,0,0]</code>.</li> |
| 13 | +</ul> |
| 14 | + |
| 15 | +<p>Return <em>the resulting array</em>.</p> |
| 16 | + |
| 17 | +<p><strong>Note</strong> that the operations are applied <strong>sequentially</strong>, not all at once.</p> |
| 18 | + |
| 19 | +<p> </p> |
| 20 | +<p><strong class="example">Example 1:</strong></p> |
| 21 | + |
| 22 | +<pre> |
| 23 | +<strong>Input:</strong> nums = [1,2,2,1,1,0] |
| 24 | +<strong>Output:</strong> [1,4,2,0,0,0] |
| 25 | +<strong>Explanation:</strong> We do the following operations: |
| 26 | +- i = 0: nums[0] and nums[1] are not equal, so we skip this operation. |
| 27 | +- 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,<strong><u>4</u></strong>,<strong><u>0</u></strong>,1,1,0]. |
| 28 | +- i = 2: nums[2] and nums[3] are not equal, so we skip this operation. |
| 29 | +- 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,<strong><u>2</u></strong>,<strong><u>0</u></strong>,0]. |
| 30 | +- 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,<strong><u>0</u></strong>,<strong><u>0</u></strong>]. |
| 31 | +After that, we shift the 0's to the end, which gives the array [1,4,2,0,0,0]. |
| 32 | +</pre> |
| 33 | + |
| 34 | +<p><strong class="example">Example 2:</strong></p> |
| 35 | + |
| 36 | +<pre> |
| 37 | +<strong>Input:</strong> nums = [0,1] |
| 38 | +<strong>Output:</strong> [1,0] |
| 39 | +<strong>Explanation:</strong> No operation can be applied, we just shift the 0 to the end. |
| 40 | +</pre> |
| 41 | + |
| 42 | +<p> </p> |
| 43 | +<p><strong>Constraints:</strong></p> |
| 44 | + |
| 45 | +<ul> |
| 46 | + <li><code>2 <= nums.length <= 2000</code></li> |
| 47 | + <li><code>0 <= nums[i] <= 1000</code></li> |
| 48 | +</ul> |
0 commit comments