当前位置:网站首页>leetCode-1089: 复写零
leetCode-1089: 复写零
2022-06-24 09:43:00 【文丑颜不良啊】
题目描述
给你一个长度固定的整数数组 arr,请你将该数组中出现的每个零都复写一遍,并将其余的元素向右平移。
注意:请不要在超过该数组长度的位置写入元素。
要求:请对输入的数组就地进行上述修改,不要从函数返回任何东西。
示例
示例 1:
输入:[1,0,2,3,0,4,5,0]
输出:null
解释:调用函数后,输入的数组将被修改为:[1,0,0,2,3,0,0,4]
示例 2:
输入:[1,2,3]
输出:null
解释:调用函数后,输入的数组将被修改为:[1,2,3]
解题过程
思路及步骤
(1)借助虚拟数组来完成,虚拟数组的大小元原数组 arr 相同,变量 j 用来记录虚拟数组中的数组元素个数;
(2)使用指针 i 去遍历原数组 arr,如果 arr[i] != 0,则 i 和 j 同时自增 1,如果 arr[i] == 0,则此时便需要进行复写 0 的操作,所以 j 自增 2,i 自增 1,当 j >= arr.length 时停止遍历;
(3)此时 i 即为对原数组进行复写 0 操作之后会出现在虚拟数组中的最后一个元素的下标(注意理解);
(4)因为 j 表示虚拟数组中的元素个数,并不是下标,所以还需要对 j 进行自减 1 操作,需要特殊情况:当原数组 arr 的最后一个元素为 0 并且复写 0 之后 j > arr.length 了,则此时需要对 j 自减 2,同时给 arr[j] 赋值为 0,之后再进行一次 j 自减 1,i 自减 1 操作;
(5)之后从右到左进行复写 0 操作,如果 arr[i] == 0,则 arr[j] 和 arr[j - 1] 都赋值为 0,并进行 j 自减,2,i 自减 1 操作;如果 arr[i] != 0,则将当前 arr[i] 的值赋值给 arr[j],同时进行 i 和 j 的自减 1 即可
代码展示
public class DuplicateZeros {
public void duplicateZeros(int[] arr) {
// 虚拟数组, 找出会出现在虚拟数组中的最后一个元素, 即 i
int i = 0;
int j = 0;
for (; i < arr.length; i++) {
if (arr[i] == 0) {
j += 2;
} else {
j++;
}
if (j >= arr.length) {
break;
}
}
// 处理原数组中最后一个元素等于 0 且复写之后越界的特殊情况
if (j > arr.length) {
if (arr[i] == 0) {
j = j - 2;
arr[j] = 0;
j--;
i--;
}
} else {
j--;
}
//System.out.println("i = " + i + ", j = " + j);
// 从右到左, 进行复写操作
while (j >= 0) {
arr[j] = arr[i];
if (arr[i] == 0) {
arr[j - 1] = arr[i];
j--;
}
i--;
j--;
}
}
public static void main(String[] args) {
int[] arr = {
1,0,2,3,0,4,5,0};
new DuplicateZeros().duplicateZeros(arr);
}
}
边栏推荐
- 上升的气泡canvas破碎动画js特效
- SQL Sever关于like操作符(包括字段数据自动填充空格问题)
- 使用swiper左右轮播切换时,Swiper Animate的动画失效,怎么解决?
- numpy.logical_and()
- uniapp实现禁止video拖拽快进
- 数组无缝滚动demo
- Tutorial (5.0) 08 Fortinet security architecture integration and fortixdr * fortiedr * Fortinet network security expert NSE 5
- numpy.linspace()
- Groovy obtains Jenkins credentials through withcredentials
- 植物生长h5动画js特效
猜你喜欢

SQL Sever关于like操作符(包括字段数据自动填充空格问题)

机器学习——主成分分析(PCA)

GeoGebra 实例 时钟

小程序学习之获取用户信息(getUserProfile and getUserInfo)

411 stack and queue (20. valid parentheses, 1047. delete all adjacent duplicates in the string, 150. inverse Polish expression evaluation, 239. sliding window maximum, 347. the first k high-frequency

Cookie encryption 4 RPC method determines cookie encryption

Groovy obtains Jenkins credentials through withcredentials

自定义kindeditor编辑器的工具栏,items即去除不必要的工具栏或者保留部分工具栏

Indexeddb local storage, homepage optimization

简单的价格表样式代码
随机推荐
How to solve multi-channel customer communication problems in independent stations? This cross-border e-commerce plug-in must be known!
416-二叉树(前中后序遍历—迭代法)
SQL sever基本数据类型详解
保健品一物一码防窜货营销软件开发
Indexeddb local storage, homepage optimization
el-table表格的拖拽 sortablejs
How to manage massive network infrastructure?
Thinkphp5 clear the cache cache, temp cache and log cache under runtime
利用pandas读取SQL Sever数据表
Mise en œuvre du rendu de liste et du rendu conditionnel pour l'apprentissage des applets Wechat.
学习使用php对字符串中的特殊符号进行过滤的方法
解决Deprecated: Methods with the same name as their class will not be constructors in报错方案
Top issue tpami 2022! Behavior recognition based on different data modes: a recent review
Record the range of data that MySQL update will lock
Binary tree part I
引擎国产化适配&重构笔记
ByteDance Interviewer: talk about the principle of audio and video synchronization. Can audio and video be absolutely synchronized?
Open Oracle server under Linux to allow remote connection
Wechat cloud hosting launch public beta: in the appointment of the publicity meeting
Geogebra instance clock