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/ 目录 题目描述 题目大意 解题方法 保存已经访问过的数字 链表成环 二分查找 日期 题目 ...
随机推荐
- hive三种调用方式
一.hive -e ‘sql语句’ (shell命令) 适合比较短的sql语句调用,优点是可以直接在shell中调用静音模式 -S 在执行HiveQL过程中,不在显示器输出MR的执行过程hive -S ...
- vue、Element 点击按钮以弹窗形式预览pdf文件
直接上代码吧 <div > <el-button type="success" size="small" @click="isVie ...
- 什么是URL百分号编码?
㈠什么是URL 统一资源定位系统(uniform resource locator;URL)是因特网的万维网服务程序上用于指定信息位置的表示方法. ㈡URL编码 url编码是一种浏览器用来打包表单输入 ...
- bat脚本休眠时间技巧
例子: @echo off set /a i=1 :c adb connect 192.168.1.109&ping -n 1 123.45.67.89 -w 10000>nul if ...
- HDU 5726 GCD (2016多校、二分、ST表处理区间GCD、数学)
题目链接 题意 : 给出一个有 N 个数字的整数数列.给出 Q 个问询.每次问询给出一个区间.用 ( L.R ) 表示.要你统计这个整数数列所有的子区间中有多少个和 GCD( L ~ R ) 相等.输 ...
- jeecg中自定义dialog,实现窗体的弹出
自定一个dialog,在子窗体中写一个方法,然后通过iframe进行调取function createwindowoktext(title, addurl,width,height,oktext,ca ...
- 数据分析师面经一(bk)
2019年第一个数据分析面试: 先说一下心理感受,在BOSS多次看到这个岗位了,但是 呢一直没勇气去投这个岗位.首先毕竟是一个知名企业一万+人的公司,心里多少底气不足(小公司待习惯了吧),而且看岗位要 ...
- 使用conda进行本地安装
1. 由于安装源被墙转换为本地安装 例如要安装 boost-1.59版本,因为在默认源中没有,我们指定了特定源进行安装 conda install -c menpo dlib=boost-1.59 然 ...
- 协程系列之Event Loops
Event Loops 事件循环 事件是由程序的一部分在特定条件下发出的消息,循环是在某种条件下完成并执行某个程序直到它完成的构造,因此,事件循环是一个循环,它允许用户订阅事件传输并注册处理程序/回调 ...
- IntelliJ IDEA工具增加test测试方法,报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing错误
是因为我在IntelliJ IDEA中,通过plugins增加 插件的时候,在 增加的测试类是junit4.12,改版本的jar包不再包含hamcrest-library.jar .我是通过将自己的项 ...