这是小川的第392次更新,第423篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第255题(顺位题号是1089)。给定一个固定长度的整数数组arr,复制每次出现的零,将剩余的元素向右移动。

请注意,不会写入超出原始数组长度的元素。

对输入数组进行上述修改,不要从函数返回任何内容。

例如:

输入:[1,0,2,3,0,4,5,0]

输出:null

说明:调用函数后,输入数组被修改为:[1,0,0,2,3,0,0,4]

输入:[1,2,3]

输出:null

说明:调用函数后,输入数组被修改为:[1,2,3]

注意

  • 1 <= arr.length <= 10000

  • 0 <= arr [i] <= 9

02 第一种解法

新建一个List,遍历arr中的元素,如果为0,添加两次到List中,然后将List中的前n个元素回写到arr中,narr的长度。

public void duplicateZeros(int[] arr) {
List<Integer> list = new ArrayList<Integer>();
for (int num : arr) {
if (num == 0) {
list.add(0);
}
list.add(num);
}
for (int i=0; i<arr.length; i++) {
arr[i] = list.get(i);
}
}

03 第二种解法

我们也可以不使用List,将arr复制一份出来,创建一个索引,遍历复制数组,将元素回写到arr中,遇到0就重复赋值一次。

public void duplicateZeros2(int[] arr) {
int n = arr.length, index = 0;
int[] copy = arr.clone();
for (int num : copy) {
if (index >= n) {
break;
}
if (index+1 < n && num == 0) {
arr[index++] = 0;
arr[index++] = 0;
} else {
arr[index++] = num;
}
}
}

04 第三种解法

我们也可以不使用额外的空间,通过双指针来实现。

先遍历arr,统计其中元素值为0的元素个数,记为count,从后往前遍历,一个长度为arr的长度,另外一个长度为arr的长度加count,遇到0就重复回写一次。

public void duplicateZeros3(int[] arr) {
int count = 0;
for (int num : arr) {
if (num == 0) {
count++;
}
}
int n = arr.length, n2 = n + count;
// i是原数组的索引,j是原数组的长度加count
for (int i=n-1, j= n2-1; i < j; i--, j--) {
if (arr[i] != 0) {
if (j < n) {
arr[j] = arr[i];
}
} else {
// 遇到0,再重复一次
if (j < n) {
arr[j] = arr[i];
}
j--;
if (j < n) {
arr[j] = arr[i];
}
}
}
}

05 小结

算法专题目前已连续日更超过八个月,算法题文章261+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

LeetCode.1089-重复的0(Duplicate Zeros)的更多相关文章

  1. LeetCode 1089. 复写零(Duplicate Zeros) 72

    1089. 复写零 1089. Duplicate Zeros 题目描述 给你一个长度固定的整数数组 arr,请你将该数组中出现的每个零都复写一遍,并将其余的元素向右平移. 注意:请不要在超过该数组长 ...

  2. 【Leetcode_easy】1089. Duplicate Zeros

    problem 1089. Duplicate Zeros 题意: solution: 其中关于虚拟新数组的下标的计算还是有点迷糊... class Solution { public: void d ...

  3. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  4. [LeetCode]1089. Duplicate Zeros

    Given a fixed length array arr of integers, duplicate each occurrence of zero, shifting the remainin ...

  5. 【leetcode】1089. Duplicate Zeros

    题目如下: Given a fixed length array arr of integers, duplicate each occurrence of zero, shifting the re ...

  6. [Leetcode 217&219]寻找数组中的重复值Contains Duplicate I & II

    [题目1] Given an array of integers, find if the array contains any duplicates. Your function should re ...

  7. LeetCode 217:存在重复元素 Contains Duplicate

    题目: 给定一个整数数组,判断是否存在重复元素. Given an array of integers, find if the array contains any duplicates. 如果任何 ...

  8. [Swift]LeetCode609. 在系统中查找重复文件 | Find Duplicate File in System

    Given a list of directory info including directory path, and all the files with contents in this dir ...

  9. 【LeetCode】287. Find the Duplicate Number 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存已经访问过的数字 链表成环 二分查找 日期 题目 ...

随机推荐

  1. phpStudy环境下composer的安装

    前言 原来是做php开发的,现在转行前端工程师,因为很久没有接触了,可能会有其他问题,这里简单记录一下,后期遇到什么问题再进行更新~ 话说下载特别慢所以这里给个网盘链接Composer-Setup.e ...

  2. js中for..of..和迭代器

    for..of是ES6中引入的新特性,它主要的作用是:循环一个可迭代的对象. 它可以循环遍历,数组.字符串.Set对象等等 示例一: let str = 'hello' for (item of st ...

  3. 记录一个关于 Document.on绑定事件后,导致页面卡顿的情况

    假设当前页面的js文件中有如下函数: function A(){ function B(); } function B(){ $(document).on("click",&quo ...

  4. JavaMail应用--通过javamail API实现在代码中发送邮件功能

    JavaMail应用   在日常开发中,可能会引用到发邮件功能,例如在持续集成中,自动化测试运行完毕,自动将测试结果以报表的形式发送邮件给相关人.那么在Java中如何实现发邮件呢? 在java EE ...

  5. hdu 5733 tetrahedron 四面体内切球球心公式

    tetrahedron Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  6. 2019.12.12网页设计大赛&2019.12.13程序设计大赛观后感

    有幸参加了一次网页设计大赛和程序设计大赛,其实在大一的时候就参加过一次程序设计大赛,那时候也没怎么听,现在又有了一次机会来听,这次就认真的听了这两次的比赛,也有很多的感悟. 1.要学习完成一个任务的多 ...

  7. [ML] Gradient Boost

    参考链接: 1. https://medium.com/@cwchang/gradient-boosting-%E7%B0%A1%E4%BB%8B-f3a578ae7205 2. https://zh ...

  8. HDU 5858 Hard problem ——(计算几何)

    其实这题最多是个小学奥数题- -,,看到别人博客各显神通,也有用微积分做的(我也试了一下,结果到最后不会积...). 思路如下(这两张图是网上找来的): 然后就很简单了,算三角形面积可以用海伦公式,也 ...

  9. 7.1 Spring原理

    7.1 Spring原理 一.spring是什么?(IOC.AOP.MVC) Spring是一个基于IOC和AOP的结构J2EE系统的框架 , 1.1 IOC 控制反转 是Spring的基础,Inve ...

  10. TCP主动打开 之 第三次握手-发送ACK

    假定客户端执行主动打开,并且已经收到服务器发送的第二次握手包SYN+ACK,在经过一系列处理之后,客户端发送第三次握手包ACK到服务器:其流程比较简单,主要是分配skb,初始化ack包并发送:需要注意 ...