当前位置:网站首页>LeetCode #283.移动零
LeetCode #283.移动零
2022-07-29 05:24:00 【张楚明ZCM】
题目截图

方法一:两次遍历
利用两次循环,第一次循环记录数组中非零的部分并覆盖原数组。记录数组中零元素个数。
第二次循环从n-count开始,将数组末尾部分的元素赋值为零。
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完整测试代码
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()方法二:双指针
设置两个指针,刚开始的时候都指向位置0处的元素。碰到非零元素则左右两个指针交换元素,然后同时右移两个指针。碰到零元素则只移动右指针,此时左指针停留在零元素的位置上。直到右指针找到非零元素后再次交换元素,然后同时右移。
当左右指针指向同一位置的时候,交换元素等价于没有操作。
当做指针发现移动到零元素上时,一直移动右指针后再交换,意味着不断的寻找后面的非零元素填充前面的位置。
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 += 1或者把right += 1提到判断语句外面
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.4GHz 无线收发芯片--Ci24R1
- 【软件工程之美 - 专栏笔记】23 | 架构师:不想当架构师的程序员不是好程序员
- 链表--------------------尾插法
- 【软件工程之美 - 专栏笔记】“一问一答”第2期 | 30个软件开发常见问题解决策略
- QT learning notes - Import and export of Excel
- scanBasePackages扫包范围配置
- mavan中的plugin位置
- 给二维表添加时间序列索引
- Huawei cloud 14 day Hongmeng device development -day5 drive subsystem development
- 循环链表和双向链表
猜你喜欢

基于AD9850的多功能信号发生器

2022 spring recruit - Hesai technology FPGA technology post (one or two sides, collected from: Digital IC workers and FPGA Explorers)

【软件工程之美 - 专栏笔记】20 | 如何应对让人头疼的需求变更问题?

STM32FF030 替代国产单片机——DP32G030

Huawei cloud 14 days Hongmeng device development -day1 environment construction

SimpleFOC调参3-PID参数整定攻略

基于msp430f2491的proteus仿真

SimpleFOC调参2-速度、位置控制

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

防爆倾角传感器应用于LNG液化天然气安全作业
随机推荐
IDEA 实用快捷键 新手必看
FT232替代GP232RL USB-RS232转换器芯片国产化应用
STM32: mcnamu wheel tracking task (library function program code)
STM32 MDK(Keil5) Contents mismatch错误总结
八大排序-----------------堆排序
DP1332E 多协议高度集成非接触式读写芯片
SimpleFOC调参3-PID参数整定攻略
Pit avoidance: about the interconnection of two hc-05 master-slave integrated Bluetooth modules, there is no connection problem
leetcode刷题笔记 452. Minimum Number of Arrows to Burst Balloons (Medium) 452.用最少数量的箭引爆气球(中等)
clickhouse 导入CSV失败 不报错但是无数据
SimpleFOC调参1-力矩控制
2022 spring move - core technology FPGA development post pen test question (original question and experience)
【软件工程之美 - 专栏笔记】20 | 如何应对让人头疼的需求变更问题?
数学建模心得
顺序表和链表
NOI Online 2022普及组 题解&个人领悟
Ml self study notes 5
FPGA based: moving target detection (schematic + source code + hardware selection, available)
Jingwei Qili: development of heart rate and blood oxygen module based on hmep060 (1: FPGA sends multi bit instructions)
IDEA安装scala