当前位置:网站首页>【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]
边栏推荐
- folium地图无法显示的问题,临时性解决方案如下
- Steps for Navicat to create a new database
- folium,确诊和密接轨迹上图
- Tidb environment and system configuration check
- . Net again! Happy 20th birthday
- How to solve the problem of database content output
- LeetCode刷题——验证二叉树的前序序列化#331#Medium
- 03_ Linear table_ Linked list
- 终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)
- Libcurl Lesson 13 static library introduces OpenSSL compilation dependency
猜你喜欢

怎样从微信返回的json字符串中截取某个key的值?

Set set you don't know

19_Redis_宕机后手动配置主机

Leetcode skimming - remove duplicate letters 316 medium

02_ Linear table_ Sequence table

How to find a sense of career direction

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

Custom exception

How does the computer set up speakers to play microphone sound

彻底弄懂浏览器强缓存和协商缓存
随机推荐
15_Redis_Redis.conf详解
Party History Documentary theme public welfare digital cultural and creative products officially launched
高考分数线爬取
密码学基础知识
Bing.com網站
How does the computer set up speakers to play microphone sound
17_Redis_Redis发布订阅
04.进入云原生后的企业级应用构建的一些思考
03_线性表_链表
LeetCode刷题——去除重复字母#316#Medium
02_线性表_顺序表
数据分析常见的英文缩写(一)
Steps for Navicat to create a new database
How to solve the problem of database content output
12_ Redis_ Bitmap_ command
搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!
Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
Topology architecture of the minimum deployment of tidb cluster
Redux - detailed explanation
基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测