问题描述:

Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.

Example:

Input: [0,1,0,3,12]
Output: [1,3,12,0,0]

Note:

  1. You must do this in-place without making a copy of the array.
  2. Minimize the total number of operations.

思路:

主要是list的sort()函数的应用

代码:

 def moveZeroes(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
nums.sort(key = lambda x: x ==0)

key是sort()函数中的一个参数,该参数指定排序的元素。当前代码中排序元素就是nums中所有0元素,到最后所有0元素一组(新),除去0元素的其他元素一组(老),最终默认按照老-新的顺序将nums重新排序

Python3解leetcode Binary Tree PathsAdd DigitsMove Zeroes的更多相关文章

  1. Python3解leetcode Binary Tree PathsAdd Digits

    问题描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...

  2. Python3解leetcode Binary Tree Paths

    问题描述: Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. E ...

  3. Python3解leetcode Same Tree

    问题描述: Given two binary trees, write a function to check if they are the same or not. Two binary tree ...

  4. Python3解leetcode Symmetric Tree

    问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...

  5. Python3解leetcode N-ary Tree Level Order Traversal

    问题描述: Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to ...

  6. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  7. LeetCode: Binary Tree Traversal

    LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...

  8. Python3解leetcode Average of Levels in Binary Tree

    问题描述: Given a non-empty binary tree, return the average value of the nodes on each level in the form ...

  9. Python3解leetcode Lowest Common Ancestor of a Binary Search Tree

    问题描述: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in ...

随机推荐

  1. react-navigation 实战

    npm install --save react-navigation 1.测试TabNavigator.StackNavigator和DrawerNavigator (1)新建HomeScreen. ...

  2. 连接超时(connect timed out)和读取超时(Read timed out)

    设置连接超时和读取超时方法: RequestConfig config=RequestConfig.custom() .setConnectTimeout(10000) // 设置连接超时时间 10秒 ...

  3. Vagrant 手册之 Provisioning - file 配置程序

    原文地址 Provisioner 命令:"file" 通过 file 配置程序可以上传宿主机的文件或目录到虚拟机中. 使用场景:将宿主机的 ~/.gitconfig 复制到虚拟机中 ...

  4. php远程抓取(下载)文件到本项目指定目录中

    function httpcopy($url, $file="", $timeout=60) { $file = empty($file) ? pathinfo($url,PATH ...

  5. HTML--JS 定时刷新、时钟、倒计时

    <html> <head> <title>定时刷新时间</title> <script language="JavaScript&quo ...

  6. 排序算法三:堆排序(Heapsort)

    堆排序(Heapsort)是一种利用数据结构中的堆进行排序的算法,分为构建初始堆,减小堆的元素个数,调整堆共3步. (一)算法实现 protected void sort(int[] toSort) ...

  7. vue使用vue-router beforEach实现判断用户登录跳转路由筛选

    vue使用vue-router beforEach实现判断用户登录跳转路由筛选 :https://www.colabug.com/3306814.html 在开发webApp的时候,考虑到用户体验,经 ...

  8. luoguP1081 开车旅行 题解(NOIP2012)

    这道题是真滴火!(一晚上加一节信息课!) 先链接一下题目:luoguP1081 开车旅行 首先,这个预处理就极其变态,要与处理出每一个点往后走A会去哪里,B会去哪里.而且还必须O(nlogn)给它跑出 ...

  9. linux配置mysql数据库远程连接失败

    今天配置linux下mysql数据库可以远程访问的问题,百度这方面的资料有很多,但是方法都一样,都试过了却未能解决,记录一下 第一步:在/etc/mysql/my.cnf下找到bind-address ...

  10. CSRF相关

    CSRF原理 第一次获取页面的时候浏览器返回一个随机字符串,之后提交数据的时候需要把到这个字符串去提交,不然会报错 返回的时候还会把这个字符串放到cookie里面, 使用form提交时候: {% cs ...