首页 > 其他分享 >leetcode-1089-easy

leetcode-1089-easy

时间:2023-03-29 20:58:33浏览次数:42  
标签:count arr 数组 int 1089 length easy array leetcode

Duplicate Zeros

Given a fixed-length integer array arr, duplicate each occurrence of zero, shifting the remaining elements to the right.

Note that elements beyond the length of the original array are not written. Do the above modifications to the input array in place and do not return anything.

Example 1:

Input: arr = [1,0,2,3,0,4,5,0]
Output: [1,0,0,2,3,0,0,4]
Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4]
Example 2:

Input: arr = [1,2,3]
Output: [1,2,3]
Explanation: After calling your function, the input array is modified to: [1,2,3]
Constraints:

1 <= arr.length <= 104
0 <= arr[i] <= 9

思路一:遍历数组,统计扩展后的长度,生成扩展后的数组,然后把值复制到原来的数组上。刚开始想实现在原来数组上修改,写出来发现总有 bug,越来越复杂,最后直接用最简单的方案

    public void duplicateZeros(int[] arr) {
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == 0) {
                count += 2;
            } else {
                count++;
            }
        }

        int[] clone = new int[count];
        int idx = 0;
        for (int num : arr) {
            if (num == 0) {
                idx += 2;
            } else {
                clone[idx++] = num;
            }
        }

        System.arraycopy(clone, 0, arr, 0, arr.length);
    }

思路二:先算出一个 end index,从数组后面往前赋值

标签:count,arr,数组,int,1089,length,easy,array,leetcode
From: https://www.cnblogs.com/iyiluo/p/17270263.html

相关文章

  • leetcode-1317-easy
    ConvertIntegertotheSumofTwoNo-ZeroIntegersNo-Zerointegerisapositiveintegerthatdoesnotcontainany0initsdecimalrepresentation.Givenani......
  • leetcode-1009-easy
    ComplementofBase10IntegerThecomplementofanintegeristheintegeryougetwhenyouflipallthe0'sto1'sandallthe1'sto0'sinitsbinaryreprese......
  • 【LeetCode】35.搜索插入位置
    题目描述  解法思路:二分查找classSolution{public:intsearchInsert(vector<int>&nums,inttarget){intleft=0,right=nums.size()-1......
  • 【LeetCode】278.第一个错误的版本
    题目描述  解法思路:二分查找注意:当第一个 isBadVersion(mid)的结果为true时,得到第一个错误的版本//TheAPIisBadVersionisdefinedforyou.//boolisBadVe......
  • leetcode 176
    leetcode176第二高的薪水,查第二高的人的信息1、使用ifnull(exp1,exp2)函数,limitoffset子句 selectifnull((selectdistinctsalaryfromEmployeeorderb......
  • LeetCode 101.对称二叉树
    1.题目:给你一个二叉树的根节点 root ,检查它是否轴对称。 示例1:输入:root=[1,2,2,3,4,4,3]输出:true2.代码:方法一:递归实现/***Definitionforabinarytreenode.......
  • #yyds干货盘点# LeetCode程序员面试金典:盛最多水的容器
    题目:给定一个长度为n的整数数组 height 。有 n 条垂线,第i条线的两个端点是 (i,0) 和 (i,height[i]) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可以......
  • #yyds干货盘点# LeetCode面试题:不同路径
    1.简述:一个机器人位于一个mxn 网格的左上角(起始点在下图中标记为“Start”)。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Fin......
  • Leetcode81. 搜索旋转排序数组 II
    classSolution{public:boolcheck(vector<int>&nums,inttarget,intl,intr)//[l,r]区间查找target{while(l<r){intmid=(......
  • 3月更新!EasyOps®全平台超10项功能升级,您的需求在其中~
    又到了每月产品盘点时刻,12大新功能上线和升级优化,涉及Hyperlnsight超融合持续观测平台、DevOps持续交付平台、AutoOps自动化运维平台、ITSM服务平台,在不断的技术创新过程中,......