2460. Apply Operations to an Array
Easy
You are given a 0-indexed array nums
of size n
consisting of non-negative integers.
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
:
If
nums[i] == nums[i + 1]
, then multiplynums[i]
by2
and setnums[i + 1]
to0
. Otherwise, you skip this operation.
After performing all the operations, shift all the 0
's to the end of the array.
For example, the array
[1,0,2,0,0,1]
after shifting all its0
's to the end, is[1,2,1,0,0,0]
.
Return the resulting array.
Note that the operations are applied sequentially, not all at once.
Example 1:
Example 2:
Constraints:
2 <= nums.length <= 2000
0 <= nums[i] <= 1000
解題
Runtime: 7 ms, faster than 100%
Memory Usage: 3.2 MB, less than 100%
Last updated