当前位置:网站首页>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
边栏推荐
- 拨号服务器是什么,有什么用处?
- Is the sinking coffee industry a false prosperity or the eve of a broken situation?
- Leetcode39 combined sum
- 简洁开源的一款导航网站源码
- [Leetcode每日一题]396. 旋转函数
- Analyze the advantages and disadvantages of tunnel proxy IP.
- 013_基于Session实现短信验证码登录流程分析
- Why is one plus one equal to two
- 代理IP可用率是不是等同于代理IP的效率?
- Makefile文件是什么?
猜你喜欢

Basic knowledge of software testing, you can meet the interviewer after reading it

Is it better to use a physical machine or a virtual machine to build a website?

拨号vps会遇到什么问题?

Heap overflow of kernel PWN basic tutorial

什么是代理IP池,如何构建?
![[hands on learning] network depth v2.1 Sequence model](/img/51/0de4c7972a99151007a8f27f351c83.png)
[hands on learning] network depth v2.1 Sequence model

Under the pressure of sales, domestic mobile phones began to reduce prices, but they haven't put down their final face

RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimensio

Batch multiple files into one hex

一些使用代理IP的小技巧。
随机推荐
2018 China Collegiate Programming Contest - Guilin Site J. stone game
MySQL basic record
ESP32使用freeRTOS的消息队列
What are the test steps of dynamic proxy IP?
Analyze the advantages and disadvantages of tunnel proxy IP.
[experience tutorial] Alipay balance automatically transferred to the balance of treasure how to set off, cancel Alipay balance automatically transferred to balance treasure?
002_Redis_String类型常见的操作命令
89 logistic回归用户画像用户响应度预测
Some tips for using proxy IP.
RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimensio
关于局域网浅谈
单片机和4G模块通信总结(EC20)
How to change the size of SVG pictures without width in openlayer
What is a dial-up server and what is its use?
Leetcode39 combined sum
如何设置电脑ip?
2022.4.20-----leetcode. three hundred and eighty-eight
Why is one plus one equal to two
校园转转二手市场源码
Network jitter tool clumsy