LeetCode 1089. Duplicate Zeros

这篇具有很好参考价值的文章主要介绍了LeetCode 1089. 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

这题要求把一个数组中所有的0后面都加一个0,给我看懵了。

首先第一个想法是java里要怎么extend一个数组……然后仔细看了一下题才发现,它让你加了0以后超出数组长度范围的直接不管了,于是,还是不会。于是,就去直接抄答案了。

这个解法比较清晰直观易懂:https://leetcode.com/problems/duplicate-zeros/solutions/312743/java-c-o-n-o-1/

首先先计算一共有多少0,就能知道需要extend这个数组多长。然后巧妙的地方来了。如果我们从后往前来insert 0的话,就不需要像从前往后那样每次都要把后面所有的数字都移动一遍,大大节省了时间。那么问题又来了,从后面开始的话,那最后面的几个数字在加0以后大概率要被遗留在数组外了,咋整?那遗留在数组外就遗留呗,不往里写就完事了,妙啊。

于是搞懂思路以后代码写起来也比较容易了。相当于还是two pointers,一个指向加完0以后的末尾,一个指向原数组的末尾,把原数组的末尾往加完0以后的末尾copy就完事了,就是需要判断一下如果加完0以后的末尾是溢出的部分就不往里写。文章来源地址https://www.toymoban.com/news/detail-679729.html

class Solution {
    public void duplicateZeros(int[] arr) {
        int count = 0;
        for (int num : arr) {
            if (num == 0) {
                count++;
            }
        }

        int newLast = arr.length + count - 1;
        int oldLast = arr.length - 1;
        while (newLast >= 0 && oldLast >= 0) {
            if (newLast < arr.length) {
                arr[newLast] = arr[oldLast];
            }
            newLast--;
            if (arr[oldLast] == 0) {
                if (newLast < arr.length) {
                    arr[newLast] = 0;
                }
                newLast--;
            }
            oldLast--;
        }
    }
}

到了这里,关于LeetCode 1089. Duplicate Zeros的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode287. Find the Duplicate Number

    Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive. There is only one repeated number in nums, return this repeated number. You must solve the problem without modifying the array nums and uses only constant extra space. Example 1: Input: nums = [1,3,4,2,2] Output: 2 Example 2: Input: nums = [3,1,3,4,

    2024年01月20日
    浏览(33)
  • LeetCode --- 1869. Longer Contiguous Segments of Ones than Zeros 解题报告

    Given a binary string  s , return  true  if the  longest  contiguous segment of  1 \\\' s is  strictly longer  than the  longest  contiguous segment of  0 \\\' s in  s , or return  false  otherwise . For example, in  s = \\\" 11 01 000 10\\\"  the longest continuous segment of  1 s has length  2 , and the longest continuous segment of  0 s has length 

    2024年02月15日
    浏览(25)
  • 力扣:1089. 复写零

    今日分享一道力扣经典题目,复写0! 题目如下: 题目要求是进行复写0,而且不能超过原数组长度,且只能在原地进行操作! 解决本题最好的方法就是进行双指针方法! 一、双指针算法先 找到最后一个要复写的数 ! 二、然后 从后向前进行完成复写 (因为如果要是从前向

    2024年02月06日
    浏览(23)
  • 代码随想录刷题第4天|LeetCode24、LeetCode19、LeetCode160、LeetCode142

    1、LeetCode24 两两交换链表中的节点 题目链接:24、两两交换链表中的节点 要想清楚终止条件,cur每次指向要交换的两个节点的前一个节点,cur = cur-next-next; 若链表元素个数为偶数 , 则最后时刻 cur-next = NULL; 若链表元素个数为奇数,则最后时刻 cur-next-next = NULL; 最后要返回

    2024年02月05日
    浏览(37)
  • 代码随想录刷题第6天|哈希表 LeetCode242、LeetCode349、LeetCode202、LeetCode1

    1、LeetCode242 有效的字母异位词 题目链接:242、有效的字母异位词 用哈希表,record[s[i]-\\\'a\\\']++,record[t[i]-\\\'a\\\']--,最后判断record里是否有元素不为0。 2、LeetCode349、两个数组的交集 题目链接:349、两个数组的交集 题目如果没有限制数值的大小,就无法使用数组来做哈希表。如果哈

    2024年02月06日
    浏览(49)
  • 《LeetCode》——LeetCode刷题日记

    本期,将给大家带来的是关于  LeetCode 的关于二叉树的题目讲解。 目录 (一)606. 根据二叉树创建字符串 💥题意分析  💥解题思路 (二)102. 二叉树的层序遍历 💥题意分析 💥解题思路 (三)236. 二叉树的最近公共祖先  💥题意分析 💥解题思路 首先,第一道题是关于

    2023年04月18日
    浏览(33)
  • 【LeetCode】《LeetCode 101》第六章:搜索

    深度优先搜索 和 广度优先搜索 是两种最常见的优先搜索方法,它们被广泛应用于图和树等结构中搜索。 深度优先搜索(depth-first search,DFS)在搜索到一个新的节点时,立即对该节点进行遍历;因此遍历需要用 先入后出 的栈实现,也可以用通过与栈等价的 递归 实现。 对于

    2023年04月09日
    浏览(59)
  • 【LeetCode周赛】LeetCode第359场周赛

    给你一个字符串数组 words 和一个字符串 s ,请你判断 s 是不是 words 的 首字母缩略词 。 如果可以按顺序串联 words 中每个字符串的第一个字符形成字符串 s ,则认为 s 是 words 的首字母缩略词。例如,“ab” 可以由 [“apple”, “banana”] 形成,但是无法从 [“bear”, “aardvark”

    2024年02月10日
    浏览(33)
  • 【LeetCode周赛】LeetCode第370场周赛

    一场比赛中共有 n 支队伍,按从 0 到 n - 1 编号。 给你一个下标从 0 开始、大小为 n * n 的二维布尔矩阵 grid 。对于满足 0 = i, j = n - 1 且 i != j 的所有 i, j :如果 grid[i][j] == 1,那么 i 队比 j 队 强 ;否则,j 队比 i 队 强 。 在这场比赛中,如果不存在某支强于 a 队的队伍,则认为

    2024年02月05日
    浏览(37)
  • 【LeetCode周赛】LeetCode第358场周赛

    给你一个下标从0开始的整数数组nums。请你从nums中找出和最大的一对数,且这两个数数位上最大的数字相等。 返回最大和,如果不存在满足题意的数字对,返回 -1 。 示例 1: 输入:nums = [51,71,17,24,42] 输出:88 解释: i = 1 和 j = 2 ,nums[i] 和 nums[j] 数位上最大的数字相等,且这

    2024年02月12日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包