最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

Leetcode 2460. Apply Operations to an Array

2022-11-28 18:04 作者:您是打尖兒還是住店呢  | 我要投稿

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 multiply?nums[i]?by?2?and set?nums[i + 1]?to?0. 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 its?0'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:

Input: nums = [1,2,2,1,1,0]Output: [1,4,2,0,0,0]

Explanation:

We do the following operations: - i = 0:?

nums[0] and nums[1] are not equal, so we skip this operation.

- 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].

- i = 2: nums[2] and nums[3] are not equal, so we skip this operation.?

- 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].

- 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].?

After that, we shift the 0's to the end, which gives the array [1,4,2,0,0,0].

Example 2:

Input: nums = [0,1]Output: [1,0]

Explanation: No operation can be applied, we just shift the 0 to the end.


easy 題目,不贅述;


Runtime:?6 ms, faster than?12.23%?of?Java?online submissions for?Apply Operations to an Array.

Memory Usage:?44.3 MB, less than?58.65%?of?Java?online submissions for?Apply Operations to an Array.


Leetcode 2460. Apply Operations to an Array的評論 (共 條)

分享到微博請遵守國家法律
清新县| 枝江市| 连云港市| 山丹县| 宁武县| 宝清县| 安达市| 呼伦贝尔市| 罗田县| 紫阳县| 安陆市| 若尔盖县| 保康县| 哈巴河县| 保亭| 禄丰县| 高青县| 原平市| 兴海县| 南川市| 尤溪县| 固安县| 凤冈县| 嵊州市| 通道| 慈利县| 宁乡县| 华容县| 安义县| 都兰县| 湖北省| 桐乡市| 舟曲县| 华安县| 福贡县| 南平市| 香格里拉县| 凤山市| 五河县| 彭山县| 玛沁县|