Leetcode Move Zeros
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.
For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [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.
inplace的改变一个list,首先要想到的就是可能用two pointers。每次遇到一个非零的nums[j], nums[i]和nums[j]交换,然后 i 指针右移一步
class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
if not nums:
return [] n = len(nums)
ind = 0
for i in range(n):
if nums[i] != 0:
nums[i], nums[ind] = nums[ind], nums[i]
ind += 1
Leetcode Move Zeros的更多相关文章
- LeetCode:Move Zeroes
LeetCode:Move Zeroes [问题再现] Given an array nums, write a function to move all 0's to the end of it w ...
- LeetCode 283 Move Zeros
Problem: Given an array nums, write a function to move all 0's to the end of it while maintaining th ...
- leetcode 283 Move Zeros; 27 Remove Elements; 26 Remove Duplicated from Sorted Array;
,,,,}; //把数组的值赋给vector vector<int> vec(arr, arr+sizeof(arr)/sizeof(int)); 解法一: 时间复杂度O(n) 空间复杂度 ...
- [LeetCode] Move Zeroes 移动零
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- [LeetCode283]Move Zeros将一个数组中为0的元素移至数组末尾
题目: Given an array nums, write a function to move all 0's to the end of it while maintaining the rel ...
- LeetCode283:Move Zeros
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- LeetCode——Move Zeroes
Description: Given an array nums, write a function to move all 0's to the end of it while maintainin ...
- LeetCode Move Zeroes (简单题)
题意: 给定一个整型数组nums,要求将其中所有的0移动到末尾,并维护所有非0整数的相对位置不变. 思路: 扫一遍,两个指针维护0与非0的交界,将非0的数向前赋值就行了. C++ class Solu ...
- leetcode-easy-array-283 move zeros
mycode 77.24% class Solution(object): def moveZeroes(self, nums): """ :type nums: Li ...
随机推荐
- javascript中的链表结构—双向链表
1.概念 上一个文章里我们已经了解到链表结构,链表的特点是长度不固定,不用担心插入新元素的时候新增位置的问题.插入一个元素的时候,只要找到插入点就可以了,不需要整体移动整个结构. 这里我们了解一下双向 ...
- 多个mapper location时, mybatis spring的自动扫描配置
1. MapperScannerConfigurer 里面的basePackage, 多个package用逗号分隔 2. SqlSessionFactoryBean里面的mapperLocations ...
- P3384 【模板】树链剖分
P3384 [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节 ...
- ASP.NET MVC图片上传前预览
回老家过春节,大半个月,在家的日子里,吃好睡好,人也长了3.5Kg.没有电脑,没有网络,无需写代码,工作上相关的完全放下......开心与父母妻儿过个年,那样的生活令Insus.NET现在还在留恋.. ...
- zabbix-proxy3.0.4编译安装
数据库配置: innodb_file_per_table=1 安装mysql rpm -ivh http://dev.mysql.com/get/mysql-community-release-e ...
- 获取iframe加载完毕事件
function IframeLoad(o,fn) { if (document.all) { o.attachEvent('onload', fn); } else { o.onload = fn; ...
- Android -- Adapter
体系 public interface Adapter----0层(表示继承体系中的层次) public interface ExpandableListAdapter---(无所谓层次因为没有其他 ...
- 关于BOM
BOM:浏览器对象模型 (Browser Object Model)主要定义的是JS操作浏览器的方法和属性. 大部分方法都在window下. 常用方法:(JS里面规定如果方法前面是window,win ...
- HTTP 错误 500.24 - Internal Server Error的解决方法
错误提示: 最可能的原因: system.web/identity@impersonate 设置为 true. 解决办法: 现在经典模式 连微软都几乎放弃了 原设想是为iis不断升级 提供的一种兼 ...
- js中的forEach
chrome和firefox支持数组的forEach,但不支持对象的forEach,IE啥都不支持 jquery中的$.each(ArrayOrObject,function)既可以遍历数组又可以遍历 ...