当前位置:网站首页>【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]
边栏推荐
- Common English abbreviations for data analysis (I)
- Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
- LeetCode刷题——验证二叉树的前序序列化#331#Medium
- 夏季高考文化成绩一分一段表
- Bing.com网站
- Tidb environment and system configuration check
- 飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测
- MD5加密
- Pytoch saves tensor to Mat file
- 微信支付宝账户体系和支付接口业务流程
猜你喜欢
14_Redis_乐观锁
Leetcode skimming -- count the number of numbers with different numbers 357 medium
08_ 串
Bing.com網站
Leetcode skimming -- incremental ternary subsequence 334 medium
Tidb data migration tool overview
Map introduction
Data analysis thinking analysis methods and business knowledge - business indicators
Yolo format data set processing (XML to txt)
6.12 企业内部upp平台(Unified Process Platform)的关键一刻
随机推荐
NBA player analysis
folium,确诊和密接轨迹上图
How to solve the problem of database content output
Custom exception
损失函数与正负样本分配:YOLO系列
XML Configuration File
[solution] educational codeforces round 82
Bing. Com website
高考分数线爬取
Summary of the first three passes of sqli Labs
folium地图无法显示的问题,临时性解决方案如下
Solve the problem of frequent interruption of mobaxterm remote connection
4. Data splitting of Flink real-time project
03_线性表_链表
LeetCode刷题——验证二叉树的前序序列化#331#Medium
05_ queue
02.面向容器化后,必须面对golang
15_ Redis_ Redis. Conf detailed explanation
Application of CDN in game field
彻底弄懂浏览器强缓存和协商缓存