当前位置:网站首页>Leetcode 283. move zero
Leetcode 283. move zero
2022-07-29 06:23:00 【Zhangchuming ZCM】
Title screenshot

Method 1 : Two traversal
Use two cycles , The first cycle records the non-zero part of the array and overwrites the original array . Record the number of zero elements in the array .
Second cycle from n-count Start , Assign zero to the element at the end of the array .
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
n = len(nums)
count = 0
j = 0
for i in range(0, n):
if nums[i]==0:
count += 1
else:
nums[j] = nums[i]
j += 1
for i in range(n-count, n):
nums[i] = 0Complete test code
from typing import List
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
n = len(nums)
count = 0
j = 0
for i in range(0, n):
if nums[i]==0:
count += 1
else:
nums[j] = nums[i]
j += 1
for i in range(n-count, n):
nums[i] = 0
class main:
a = Solution()
nums = [6,0,53,0,11,25,7]
a.moveZeroes(nums)
print(nums)
if __name__ == '__main__':
main()Method 2 : Double pointer
Set two pointers , At the beginning, they all point to the position 0 The element of . When encountering non-zero elements, the left and right pointers exchange elements , Then move both pointers to the right at the same time . When it encounters zero element, it only moves the right pointer , At this time, the left pointer stays at the position of the zero element . Until the right pointer finds a non-zero element, exchange elements again , Then move right at the same time .
When the left and right pointers point to the same position , Exchanging elements is equivalent to no operation .
When the pointer is found to move to the zero element , Always move the right pointer before switching , It means constantly looking for non-zero elements in the back to fill the front position .
def moveZeroes(self, nums: List[int]) -> None:
n = len(nums)
left, right = 0, 0
for right in range(0, n):
if nums[right] == 0 :
right += 1
else:
nums[right], nums[left] = nums[left], nums[right]
right += 1
left += 1Or the right += 1 Refer to the outside of the judgment statement
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
n = len(nums)
left, right = 0, 0
for right in range(0, n):
if nums[right] != 0 :
nums[right], nums[left] = nums[left], nums[right]
left += 1
right += 1边栏推荐
猜你喜欢
随机推荐
[beauty of software engineering - column notes] 17 | what is the need analysis? How to analyze?
STM32 MDK(Keil5) Contents mismatch错误总结
传统模型预测控制轨迹跟踪——波浪形轨迹(功能包已经更新)
Ml8 self study notes LDA principle formula derivation
LeetCode #13. 罗马数字转整数
Huawei cloud 14 day Hongmeng device development -day2 compilation framework
IDEA 实用快捷键 新手必看
STM32 检测信号频率
关于【链式前向星】的自学理解
SQLyog 安装和配置教程
Huawei cloud 14 day Hongmeng device development -day5 drive subsystem development
From entry to soul: how to use tb6600 single chip microcomputer to control stepping motor with high precision (42/57)
Huawei cloud 14 days Hongmeng device development -day1 environment construction
Traditional model predictive control trajectory tracking - circular trajectory (function package has been updated)
单链表面试题
#7110 数字走向2 题解
[beauty of software engineering - column notes] 19 | as a programmer, you should have product awareness
【软件工程之美 - 专栏笔记】17 | 需求分析到底要分析什么?怎么分析?
抽象类以及接口
ML7 self study notes









