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/ 目录 题目描述 题目大意 解题方法 保存已经访问过的数字 链表成环 二分查找 日期 题目 ...
随机推荐
- mysql增删查改练习
建表 班级表 create table class( cid int auto_increment unique, caption varchar(32) not null default '' )c ...
- tcpip入门的网络教程汇总
网络编程懒人入门(一):快速理解网络通信协议(上篇) http://www.52im.net/thread-1095-1-1.html TCP/IP详解学习笔记 https://www.cnblogs ...
- ip端口协议,SSHj加密连接
SSH协议----一旦linux服务器开机,SSH协议就不断的侦听22端口,一旦有连接到达,就校验连接用户名和密码: 可以理解为连接ip端口为找到一栋大楼,发送用户名和密码为向大楼保安出示证件.SSH ...
- gtid 1032同步异常处理
gtid 1032同步异常处理 .sql # 在从库中执行 ; ,); ,); ,); ,); ; mysql> select * from fudao_student_lable; +--- ...
- 4 saltstack ssh
1.salt ssh https://docs.saltstack.com/en/latest/topics/ssh/index.html 免agent的 Salt ssh 串行 执行的时候,发送给其 ...
- Python之hashlib模块的使用
hashlib模块主要的作用: 加密保护消息安全,常用的加密算法如MD5,SHA1等. 1.查看可用的算法有哪些 #!/usr/bin/env python # -*- coding: utf-8 - ...
- C#与数据库有关的控件和属性
BindingNavigator 数据记录导航 BindingSource 与数据源绑定, 常用属性:DataSource.DataMember 常用方法: DataGridView 以表格形式显示 ...
- H5 设计尺寸
750*1218 微信下 兼容 7plus 内容高度 居中 1000px 内 750*1448 微信下 兼容 iphoneX 微信导航栏高度 64px 64px = 导航栏44+状态栏20 但是现在 ...
- Reborn & Recover
高考总算是结束了 而我们的旅程却还在继续 现在姑且是个ACMer(暂定) 而我已经将近一年没写代码了 接下来是全新的开始 全新的学校 全新的未来 总之从现在开始 试着努力看看吧!
- conda程序使用
conda -c 参数 使用清华镜像时不要使用-c 参数.-c参数是anaconda的默认channel. 查询安装源中某个包的可以安装的版本 conda search -f package_name ...