首页 > 其他分享 >[LeetCode] 2460. Apply Operations to an Array

[LeetCode] 2460. Apply Operations to an Array

时间:2023-06-05 13:12:45浏览次数:46  
标签:Operations operations cur operation nums int Apply Array array

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.

Constraints:

  • 2 <= nums.length <= 2000
  • 0 <= nums[i] <= 1000

对数组执行操作。

给你一个下标从 0 开始的数组 nums ,数组大小为 n ,且由 非负 整数组成。

你需要对数组执行 n - 1 步操作,其中第 i 步操作(从 0 开始计数)要求对 nums 中第 i 个元素执行下述指令:

如果 nums[i] == nums[i + 1] ,则 nums[i] 的值变成原来的 2 倍,nums[i + 1] 的值变成 0 。否则,跳过这步操作。
在执行完 全部 操作后,将所有 0 移动 到数组的 末尾 。

例如,数组 [1,0,2,0,0,1] 将所有 0 移动到末尾后变为 [1,2,1,0,0,0] 。
返回结果数组。

注意 操作应当 依次有序 执行,而不是一次性全部执行。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/apply-operations-to-an-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是追击型双指针,如果没有做过283题,建议先做一下,本题跟283题几乎一样。

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     public int[] applyOperations(int[] nums) {
 3         int n = nums.length;
 4         for (int i = 0; i < n - 1; i++) {
 5             if (nums[i] == nums[i + 1]) {
 6                 nums[i] = nums[i] * 2;
 7                 nums[i + 1] = 0;
 8             }
 9         }
10 
11         int cur = 0;
12         for (int i = 0; i < n; i++) {
13             if (nums[i] != 0) {
14                 nums[cur] = nums[i];
15                 cur++;
16             }
17         }
18         while (cur < n) {
19             nums[cur] = 0;
20             cur++;
21         }
22         return nums;
23     }
24 }

 

相关题目

283. Move Zeroes

2460. Apply Operations to an Array

LeetCode 题目总结

标签:Operations,operations,cur,operation,nums,int,Apply,Array,array
From: https://www.cnblogs.com/cnoodle/p/17457516.html

相关文章

  • Programming: elimination array duplicate
     JavaScript1.spliceletarr=[1,2,3,5,6,4,3,2,1,1,2,3,4,5]for(leti=0;i<arr.length-1;++i){for(letj=i+1;j<arr.length;++j){if(arr[i]===arr[j]){arr.splice(j--,1)}}}c......
  • 2023年6月2日,ArrayList
    1.ArrayListpackagecom.wz.arraylist_class;importjava.util.*;publicclasstest02{/***知识点:集合中可以存储不同类型的元素*泛型*@paramargs**/publicstaticvoidmain(String[]args){ArrayList<String>list......
  • 0002-array笔记
    目录std::array的size()是编译期确定的,不可改变大小std::span和std::array区别展开查看`span`是一个轻量级的容器,可以包装任意类型和大小的连续内存区域,它并不拥有所包装的内存,只是提供了对这些内存的非拥有式视图`span`的作用是提供对一段内存的访问,而不是管理一......
  • List系列集合:ArrayList集合的底层原理
        ......
  • Arrays:自定义排序规则的方式二
          ......
  • Arrays类:自定义排序规则的方式一
         ......
  • pandas dataframe 过滤——apply最灵活!!!
    按照某特定string字段长度过滤:importpandasaspddf=pd.read_csv('filex.csv')df['A']=df['A'].astype('str')df['B']=df['B'].astype('str')mask=(df['A'].str.len()==10)&(df......
  • java.lang.ClassCastException: com.alibaba.fastjson2.JSONArray cannot be cast to
    是这样的,今天跟着写springboot项目的时候,前端登录的时候报403错误。检查了半天,是后端的问题报错代码: 第二句报错提示如下 说是JSONArray不能转String,但我这也不是JSONArry转String而是Object转String啊。网上搜了半天,用了好多种方法也没什么用最后索性死马当活马医,让它......
  • leetcode 561. Array Partition I
    Givenanarrayof2nintegers,yourtaskistogrouptheseintegersintonpairsofinteger,say(a1,b1),(a2,b2),...,(an,bn)whichmakessumofmin(ai,bi)forallifrom1tonaslargeaspossible.Example1:Input:[1,4,3,2]Output:4Explanation:......
  • leetcode 350. Intersection of Two Arrays II
    Giventwoarrays,writeafunctiontocomputetheirintersection.Example:Givennums1=[1,2,2,1],nums2=[2,2],return[2,2].Note:Eachelementintheresultshouldappearasmanytimesasitshowsinbotharrays.Theresultcanbeinanyorder.Foll......