当前位置:网站首页>Leetcode-1089: replication zero
Leetcode-1089: replication zero
2022-06-24 10:15:00 【Ugly and ugly】
Title Description
Give you a fixed length array of integers arr, Please copy every zero that appears in the array , And shift the rest of the elements to the right .
Be careful : Please do not write elements beyond the length of the array .
requirement : Please modify the input array in place , Don't return anything from a function .
Example
Example 1:
Input :[1,0,2,3,0,4,5,0]
Output :null
explain : After calling the function , The input array will be modified to :[1,0,0,2,3,0,0,4]
Example 2:
Input :[1,2,3]
Output :null
explain : After calling the function , The input array will be modified to :[1,2,3]
The problem solving process
Ideas and steps
(1) With the help of virtual arrays , The size of the virtual array is the original array arr identical , Variable j Used to record the number of array elements in a virtual array ;
(2) Use the pointer i To traverse the original array arr, If arr[i] != 0, be i and j At the same time, self increase 1, If arr[i] == 0, Then you need to copy 0 The operation of , therefore j Self increasing 2,i Self increasing 1, When j >= arr.length Stop traversal when ;
(3) here i That is, copy the original array 0 After the operation, the subscript of the last element in the virtual array will appear ( Pay attention to understanding );
(4) because j Represents the number of elements in the virtual array , Not a subscript , So we need to be right j Since the subtract 1 operation , Need special circumstances : When the original array arr The last element of is 0 And copy 0 after j > arr.length 了 , At this time, you need to j Self reduction 2, At the same time arr[j] The assignment is 0, Then do it again j Self reduction 1,i Self reduction 1 operation ;
(5) Then copy from right to left 0 operation , If arr[i] == 0, be arr[j] and arr[j - 1] All assigned to 0, And carry on j Self reduction ,2,i Self reduction 1 operation ; If arr[i] != 0, Will be the current arr[i] The value of is assigned to arr[j], At the same time i and j Self subtraction of 1 that will do
Code display
public class DuplicateZeros {
public void duplicateZeros(int[] arr) {
// Virtual array , Find the last element that will appear in the virtual array , namely 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;
}
}
// Process the last element in the original array to be equal to 0 And the special case of crossing the boundary after copying
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);
// From right to left , Perform replication operation
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);
}
}
边栏推荐
- How do novices choose the grade of investment and financial products?
- Wechat cloud hosting launch public beta: in the appointment of the publicity meeting
- Go language development environment setup +goland configuration under the latest Windows
- 415 binary tree (144. preorder traversal of binary tree, 145. postorder traversal of binary tree, 94. inorder traversal of binary tree)
- np.float32()
- web网站开发,图片懒加载
- Canvas draw picture
- 学习使用phpstripslashe函数去除反斜杠
- Indexeddb local storage, homepage optimization
- 100 GIS practical application cases (XIV) -arcgis attribute connection and using Excel
猜你喜欢

时尚的弹出模态登录注册窗口

SVG+js拖拽滑块圆形进度条

YOLOv6:又快又准的目标检测框架开源啦

Impdp leading schema message ora-31625 exception handling

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

美国电子烟巨头 Juul 遭遇灭顶之灾,所有产品强制下架

Basic operations on binary tree

使用swiper左右轮播切换时,Swiper Animate的动画失效,怎么解决?

Juul, the American e-cigarette giant, suffered a disaster, and all products were forced off the shelves

Getting user information for applet learning (getuserprofile and getUserInfo)
随机推荐
静态链接库和动态链接库的区别
el-table表格的拖拽 sortablejs
学习使用phpstripslashe函数去除反斜杠
2021-08-17
操作符详解
What are the characteristics of EDI local deployment and cloud hosting solutions?
Desktop software development framework reward
414-二叉树的递归遍历
Arbre binaire partie 1
一群骷髅在飞canvas动画js特效
Symbol. Iterator iterator
Floating point notation (summarized from cs61c and CMU CSAPP)
微信小程序學習之 實現列錶渲染和條件渲染.
Recursive traversal of 414 binary tree
SSM整合
Indexeddb local storage, homepage optimization
解决Deprecated: Methods with the same name as their class will not be constructors in报错方案
tf.contrib.layers.batch_norm
SQL Server AVG函数取整问题
微信小程序rich-text图片宽高自适应的方法介绍(rich-text富文本)