LeetCode.1089-重复的0(Duplicate Zeros)
这是小川的第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中,n为arr的长度。
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)的更多相关文章
- LeetCode 1089. 复写零(Duplicate Zeros) 72
1089. 复写零 1089. Duplicate Zeros 题目描述 给你一个长度固定的整数数组 arr,请你将该数组中出现的每个零都复写一遍,并将其余的元素向右平移. 注意:请不要在超过该数组长 ...
- 【Leetcode_easy】1089. Duplicate Zeros
problem 1089. Duplicate Zeros 题意: solution: 其中关于虚拟新数组的下标的计算还是有点迷糊... class Solution { public: void d ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- [LeetCode]1089. Duplicate Zeros
Given a fixed length array arr of integers, duplicate each occurrence of zero, shifting the remainin ...
- 【leetcode】1089. Duplicate Zeros
题目如下: Given a fixed length array arr of integers, duplicate each occurrence of zero, shifting the re ...
- [Leetcode 217&219]寻找数组中的重复值Contains Duplicate I & II
[题目1] Given an array of integers, find if the array contains any duplicates. Your function should re ...
- LeetCode 217:存在重复元素 Contains Duplicate
题目: 给定一个整数数组,判断是否存在重复元素. Given an array of integers, find if the array contains any duplicates. 如果任何 ...
- [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 ...
- 【LeetCode】287. Find the Duplicate Number 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存已经访问过的数字 链表成环 二分查找 日期 题目 ...
随机推荐
- 2 APIView与序列化组件
1.入门 1.1 参考blog 官方文档:http://www.django-rest-framework.org/tutorial/quickstart/#quickstart yuan的Blog: ...
- inner join和left join
查询所有商品(product),包含他的所有的评论(comment),包含评论下的user 要使用 SELECT * FROM product p LEFT JOIN COMMENT c ON p. ...
- VirtualBox:无法访问共享文件夹
造冰箱的大熊猫@cnblogs 2019/5/9 问题:VirtualBox中安装Linux虚拟机,设置宿主机某个文件夹为虚拟机的共享文件夹.在虚拟机中,该共享文件夹显示为“sf_×××”,打开该文件 ...
- windows驱动开发详解学习笔记
1. windows驱动分两类,NT式驱动和WDM驱动,后者支持即插即用: 2. DriverEntry是入口函数,传入参数:pDriverObject由IO管理器传入: 3. WDM驱动中,AddD ...
- delphi中Tkbmmemtable数据转成SQL脚本
unit UMemtableToSql; interface uses SysUtils, Classes, DB, kbmMemTable, Variants, Dialogs, SuperObje ...
- 关于我&留言板
在下高一OIer一枚,就读于SC的一所发展中学(ruo)校 对二次元什么的,有着淡淡的喜爱 初三的时候入了古风的坑,想变得文艺一点,可爱一点 也会听歌,但听得不多(主要是听新歌比较随缘),范围窄(古风 ...
- [题解] [bzoj2622] 深入虎穴
题解 题解 考虑到正着跑不好想, 我们尝试反向跑 以每个终点作为起点, 维护每个点的最小值和次小值(最小的被老虎ban掉了) 转移的时候用当前点的次小值去更新其所连的点的最小值和次小值 由于最小的次小 ...
- MySQL 主从复制 (CentOS 7)
1.主从复制原理 (1) 当master 服务器上的数据发生改变时,则将其改变写入二进制日志文件中: (2) slave服务器会在一定时间间隔内对 master 服务器上的二进制日志进行探测,探测其是 ...
- ssh不输入密码
要通过跳转机器远程其他的机器 不方便使用秘钥 每次都要输入密码也很烦 使用sshpass可以复制一行命令就直接登录了 我的跳板机是Centos7安装sshpass很简单 直接如下搞定 yum inst ...
- tomcat manager 配置
使用网页部署新 Web 应用程序或取消现有 Web 应用程序部署,且无需重启容器. 一.开启管理 编辑 conf/tomcat-users.xml 添加如下内容,这里用户名和密码都为 tomcat & ...