当前位置:网站首页>[leetcode] 283 move zero
[leetcode] 283 move zero
2022-07-02 15:36:00 【Crisp ~】
Given an array nums, Write a function that will 0 Move to end of array , While maintaining the relative order of non-zero elements .
Please note that , You must operate on the array in place without copying it .
Example 1:
Input : nums = [0,1,0,3,12]
Output : [1,3,12,0,0]
Example 2:
Input : nums = [0]
Output : [0]
Tips :
- 1 <= nums.length <= 104
- -231 <= nums[i] <= 231 - 1
# Stupid method
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)
# Double pointer , Speed pointer , Fast pointer fast traversal sequence value , Encountered a non-zero number covering the slow pointer position
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]
边栏推荐
- 03. Preliminary use of golang
- Beijing rental data analysis
- 【LeetCode】19-删除链表的倒数第N个结点
- 党史纪实主题公益数字文创产品正式上线
- 02_线性表_顺序表
- Pytoch saves tensor to Mat file
- 18_ Redis_ Redis master-slave replication & cluster building
- 【LeetCode】1162-地图分析
- Build your own semantic segmentation platform deeplabv3+
- 【Leetcode】167-两数之和II -输入有序数组
猜你喜欢

Bing. Site Internet

Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language

Yolo format data set processing (XML to txt)

07_ Hash

Redux - detailed explanation

SQL transaction

JVM architecture, classloader, parental delegation mechanism

搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!

14_Redis_乐观锁

SQL stored procedure
随机推荐
02. After containerization, you must face golang
10_ Redis_ geospatial_ command
党史纪实主题公益数字文创产品正式上线
yolo格式数据集处理(xml转txt)
6.12 企业内部upp平台(Unified Process Platform)的关键一刻
NBA player analysis
06_ Stack and queue conversion
【LeetCode】417-太平洋大西洋水流问题
【网络安全】网络资产收集
03.golang初步使用
02_线性表_顺序表
List set & UML diagram
【LeetCode】1162-地图分析
Bing. Com website
Download blender on Alibaba cloud image station
LeetCode刷题——递增的三元子序列#334#Medium
Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
Markdown tutorial
. Solution to the problem of Chinese garbled code when net core reads files
03_线性表_链表