Python3解leetcode Binary Tree PathsAdd DigitsMove Zeroes
问题描述:
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:
- You must do this in-place without making a copy of the array.
- 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的更多相关文章
- Python3解leetcode Binary Tree PathsAdd Digits
问题描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...
- 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 ...
- Python3解leetcode Same Tree
问题描述: Given two binary trees, write a function to check if they are the same or not. Two binary tree ...
- Python3解leetcode Symmetric Tree
问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...
- 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 ...
- LeetCode:Binary Tree Level Order Traversal I II
LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...
- LeetCode: Binary Tree Traversal
LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...
- 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 ...
- 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 ...
随机推荐
- BZOJ 5170: Fable
离散化+树状数组 求当前位之前是否有k位比它大 这样的话它就需要前移k位 剩下的按照原来的顺序依次填入 其实我觉得sort一下就可以做出来了 太久没写树状数组了 所以写了一下树状数组 #include ...
- centos 问题解决记录
在centos上用pip安装包,显示成功安装,但是用pip list去看发现实际上并没有安装? 安装用的是pip install xxx 是不行的,需要用sudo pip install xxx就可以 ...
- CDN:BootCDN
ylbtech-CDN:BootCDN BootCDN稳定.快速.免费的前端开源项目 CDN 加速服务共收录了 3351 个前端开源项目 1. 推荐返回顶部 1. bootstrap Bootstra ...
- ElasticSearch ClusterBlockException[blocked by: [FORBIDDEN/12/index read-only / allow delete (api)]锁定状态,无法插入数据
PUT /twitter/_settings { "index.blocks.read_only_allow_delete": null } 官网给出的解决办法
- day36—javascript对表格table的操作应用(一)
转行学开发,代码100天——2018-04-21 今天记录一下,JavaScript对表格table的操作应用,包括表格元素的获取,创建,删除等. 一个普通的完整表格包括以下几个部分:table-&g ...
- 【ABAP系列】SAP ABAP 工单增强
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 工单增强 ...
- 【BASIS系列】SAP 设置系统timeout时间
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[BASIS系列]SAP 设置系统timeout ...
- servlet--获取类路径下资源
context 获取真实路径(*****) 还可以使用ServletContext对象来获取Web应用下的资源,例如在hello应用的根目录下创建a.txt文件,现在想在Servlet中获取这个资源 ...
- 洛谷 P1440 求m区间内的最小值(单调队列)
题目链接 https://www.luogu.org/problemnew/show/P1440 显然是一道单调队列题目…… 解题思路 对于单调队列不明白的请看这一篇博客:https://www.cn ...
- mongodb的有关操作
mongodb的几种启动方法 https://www.cnblogs.com/LLBFWH/articles/11013791.html MongoDB 之 你得知道MongoDB是个什么鬼 Mong ...