当前位置:网站首页>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边栏推荐
- 计算机大厂面试题
- Markdown and typora
- Huawei cloud 14 day Hongmeng device development -day5 drive subsystem development
- 三国演义章节内容
- NoClassDefFoundError 处理
- Jingwei Qili: OLED character display based on hmep060 (and Fuxi project establishment demonstration)
- 【Leetcode刷题】数组2——二分查找
- 利用云打码来破解登录遇到验证码的问题
- 2022 spring move - core technology FPGA post technical aspects (one side experience)
- LeetCode #283.移动零
猜你喜欢
随机推荐
关于时间复杂度的个人看法
SQLyog 安装和配置教程
Traditional model predictive control trajectory tracking - circular trajectory (function package has been updated)
Design and implementation of QT learning notes data management system
2022 spring move - core technology FPGA development post pen test question (original question and experience)
【软件工程之美 - 专栏笔记】25 | 有哪些方法可以提高开发效率?
2.4G频段的无线收发芯片 SI24R1 问题汇总解答
LeetCode #876.链表的中间结点
利用云打码来破解登录遇到验证码的问题
UE5 纹理系统讲解及常见问题设置及解决方案
唯美girls
动态加载数据
JUC集合类不安全
Eight sorts ------------- heap sort
FPGA based: multi-target motion detection (hand-in-hand teaching ①)
[beauty of software engineering - column notes] 19 | as a programmer, you should have product awareness
【软件工程之美 - 专栏笔记】20 | 如何应对让人头疼的需求变更问题?
从头安装MYSQL(MYSQL安装文档-解压版)
markdown与Typora
[beauty of software engineering - column notes] 17 | what is the need analysis? How to analyze?









