当前位置:网站首页>LeetCode 283. Move zero (simple, array) Day12
LeetCode 283. Move zero (simple, array) Day12
2022-04-23 02:05:00 【White Code】
Title Description : Given an array nums, Write a function that will 0 Move to end of array , While maintaining the relative order of non-zero elements .
Please note that , You must operate on the array in place without copying it .
Example 1:
Input : nums = [0,1,0,3,12]
Output : [1,3,12,0,0]
Example 2:
Input : nums = [0]
Output : [0]
title :
Method 1 : Double finger needling ( Think of your own way )
1、 Traversal array ;
2、 If num[i] != 0 Continue traversal , If num[i]==0 Do the following ;
3、 Set two pointers first ,left_pointer and left_move.left_pointer The element pointing to the current array is 0 The subscript of is i,left_move Subscript pointing to the next element ;
4、 From the current left_move Start to judge whether the current element is 0, If it's zero ,left_move Continue to move backward until you reach the first one that is not 0 The elements of ;
5、 In exchange for num[left_pointer ] And nums[left_move] Value , Continue with the above , Until the end of the cycle
Method 2 : Double pointer method ( Refer to the online solution )
We define two pointers ,i Pointers and k The pointer ,i The pointer is used to traverse the entire nums Array ,k The pointer is used to place nums Array elements . Then the non 0 The elements are placed in the original relative order nums Before the array ,
The rest of the positions are set to 0. So we're done 0 The movement of elements , At the same time, non 0 The relative order of elements .
The specific process is as follows :
1、 Define two pointers i and k, initialization i = 0,k = 0.
2、i The pointer moves back , All over nums Array , If nums[i] != 0, That is to say, we have encountered non 0 Elements , At this point we will nums[i] The element is placed in nums[k] Location , meanwhile k++ The latter .
3、 The final will be k The elements after the position are assigned to 0.
author :lin-shen-shi-jian-lu-k
link :https://leetcode-cn.com/problems/move-zeroes/solution/283-yi-dong-ling-shuang-zhi-zhen-zuo-fa-6wmvs/
Code implementation :
import java.util.Arrays;
public class MoveZeros {
public static void main(String[] args) {
int[] nums = {0, 1, 0,0, 3, 12, 15, 23, 0, 19, 0};
moveZeroes1(nums);
System.out.println(Arrays.toString(nums));
}
// Method 1: Double finger needling 1
public static void moveZeroes1(int[] nums) {
// 1. If the array is empty or the length of the array is 1 The ending method
if (nums == null || nums.length == 1) {
return;
}
// 2. Traverse the array to find
for (int i = 0; i < nums.length; i++) {
// 2.1 Find an array element with a value of 0 The elements of
if(nums[i] == 0){
// 2.2 Declare two variables
int left_pointer = i, left_move = i + 1;
while (left_move < nums.length && nums[left_move] == 0){
left_move++;
}
if(left_move >= nums.length){ // The following elements are 0 You can quit
break;
}else { // Exchange elements
int temp = nums[left_move];
nums[left_move] = nums[left_pointer];
nums[left_pointer] = temp;
}
}
}
}
// Method 2: Double finger needling 2
public static void moveZeroes2(int[] nums){
// 1. If the array is empty or the length of the array is 1 The ending method
if (nums == null || nums.length == 1) {
return;
}
// 2. Set double pointer
int k = 0; // Non in record array 0 The number of
for(int i = 0; i < nums.length; i++){
if(nums[i] != 0){
nums[k++] = nums[i];
}
}
int k_zero = nums.length -k; // In the record array 0 The number of
for(int i = nums.length-1; k_zero != 0; i--){
nums[i] = 0;
k_zero--; // Every time k You have to subtract one
}
}
}
Link to the original text :https://leetcode-cn.com/problems/move-zeroes/
版权声明
本文为[White Code]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220839089354.html
边栏推荐
- RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimensio
- Some tips for using proxy IP.
- How to classify proxy IP?
- php 2022年4月20面试题整理
- What are the test steps of dynamic proxy IP?
- Shardingsphere broadcast table and binding table
- RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimensio
- Shardingsphere introduction and sub table usage
- 89 logistic回归用户画像用户响应度预测
- Is CICC fortune a state-owned enterprise and is it safe to open an account
猜你喜欢

Why is one plus one equal to two

代理IP可用率是不是等同于代理IP的效率?

我国科学家揭示突破水稻产量瓶颈新机制

easyswoole环境配置

从开源爱好者到 Apache 董事,一共分几步?

How does Axure set the content of the text box to the current date when the page is loaded

使用代理IP是需要注意什么?

假如404页面是这样的 | 每日趣闻

What businesses use physical servers?

World Book Day 𞓜 a good book that technicians should not miss (it cutting-edge technology)
随机推荐
App optimization and advanced scoreboard Part 2 [Mui + flask + mongodb]
Batch multiple files into one hex
On LAN
[leetcode daily question] 396 Rotation function
What is a makefile file?
What problems will you encounter when dialing VPS?
什么是代理IP池,如何构建?
【Chrome扩展程序】content_script的跨域问题
什么是api接口?
[assembly language] understand "stack" from the lowest point of view
【动手学深度学习V2】循环神经网络-1.序列模型
C语言实现Base64编码/解码
【dpdk】10. Dpdk DNS learning notes
Communication summary between MCU and 4G module (EC20)
Uncover floating-point operations hidden by the ARM compiler
What are the test steps of dynamic proxy IP?
NPM yarn startup error [resolved]
PID精讲
89 logistic regression user portrait user response prediction
批处理多个文件合成一个HEX