当前位置:网站首页>【LeetCode】283-移动零
【LeetCode】283-移动零
2022-07-02 12:09:00 【酥酥~】
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
请注意 ,必须在不复制数组的情况下原地对数组进行操作。
示例 1:
输入: nums = [0,1,0,3,12]
输出: [1,3,12,0,0]
示例 2:
输入: nums = [0]
输出: [0]
提示:
- 1 <= nums.length <= 104
- -231 <= nums[i] <= 231 - 1
#笨方法
class Solution(object):
def moveZeroes(self, nums):
n = len(nums)
for i in range(n):
if nums[n-i-1]==0:
tmp = nums.pop(n-i-1)
nums.append(tmp)
#双指针,快慢指针,快指针快速遍历数列值,遇到非零数覆盖慢指针位置
class Solution(object):
def moveZeroes(self, nums):
length = len(nums)
slow = 0
for fast in range(length):
if nums[fast]:
nums[slow] = nums[fast]
slow+=1
nums[slow:]=(length-slow)*[0]
边栏推荐
- 13_Redis_事务
- Pytorch 保存tensor到.mat文件
- Build your own semantic segmentation platform deeplabv3+
- Data analysis thinking analysis methods and business knowledge - business indicators
- How to conduct TPC-C test on tidb
- 20_Redis_哨兵模式
- 18_ Redis_ Redis master-slave replication & cluster building
- [solution] educational codeforces round 82
- 提前批院校名称
- Leetcode skimming -- count the number of numbers with different numbers 357 medium
猜你喜欢

17_ Redis_ Redis publish subscription

There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant

Markdown tutorial

2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)

03_线性表_链表

Redux——详解

Solution of Queen n problem

21_Redis_浅析Redis缓存穿透和雪崩

Leetcode question brushing - parity linked list 328 medium

06_栈和队列转换
随机推荐
vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
微信支付宝账户体系和支付接口业务流程
LeetCode刷题——去除重复字母#316#Medium
4. Data splitting of Flink real-time project
Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board
How to test tidb with sysbench
18_ Redis_ Redis master-slave replication & cluster building
Let your HMI have more advantages. Fet-g2ld-c core board is a good choice
怎样从微信返回的json字符串中截取某个key的值?
Semantic segmentation learning notes (1)
How to find a sense of career direction
SQL stored procedure
Summary of the first three passes of sqli Labs
08_ 串
How to intercept the value of a key from the JSON string returned by wechat?
Apprendre le Code de la méthode de conversion du calendrier lunaire grégorien en utilisant PHP
14_Redis_乐观锁
SQL transaction
Application of CDN in game field
03_线性表_链表