当前位置:网站首页>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边栏推荐
- 【软件工程之美 - 专栏笔记】“一问一答”第2期 | 30个软件开发常见问题解决策略
- JUC concurrent knowledge points
- 【软件工程之美 - 专栏笔记】30 | 用好源代码管理工具,让你的协作更高效
- STM32FF030 替代国产单片机——DP32G030
- 网络安全学习篇
- STM32 printf问题总结 semihosting microLIB理解
- Huawei cloud 14 day Hongmeng device development -day3 kernel development
- LeetCode #14. 最长公共前缀
- clickhouse 导入CSV失败 不报错但是无数据
- 数论:px+py 不能表示的最大数为pq-p-q的证明
猜你喜欢

LeetCode #557.反转字符串中的单词 III

FPGA based: moving target detection (supplementary simulation results, available)

Traditional model predictive control trajectory tracking - circular trajectory (function package has been updated)

Joiner.on和stream().map联合使用技巧

UE5 纹理系统讲解及常见问题设置及解决方案

2022暑初二信息竞赛学习成果分享1

STM32 检测信号频率

【软件工程之美 - 专栏笔记】22 | 如何为项目做好技术选型?

【Leetcode刷题】数组3——分治

Maya ACES工作流程配置(Arnold 及 RedShift 贴图配置规范-还原出SP-Aces流程下贴图正确的效果) PS还原Aces流程下渲染的图
随机推荐
链表--------------------尾插法
JUC collection class is unsafe
LeetCode #19.删除链表的倒数第N个结点
ArduinoIDE + STM32Link烧录调试
STM32: mcnamu wheel tracking task (library function program code)
NoClassDefFoundError 处理
传统模型预测控制轨迹跟踪——圆形轨迹(功能包已经更新)
LeetCode #7.整数反转
STM32 MDK(Keil5) Contents mismatch错误总结
【Leetcode刷题】数组2——二分查找
计算机大厂面试题
Markdown and typora
动态加载数据
【Leetcode刷题】数组3——分治
[beauty of software engineering - column notes] 16 | how to write project documents?
给二维表添加时间序列索引
封装——super关键字
LeetCode #344.反转字符串
传统模型预测控制轨迹跟踪——波浪形轨迹(功能包已经更新)
LeetCode #83. 删除排序链表中的重复元素