Move Zeroes
https://leetcode.com/problems/move-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.
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
class Solution: # 无法改变参数,必须重新赋值,why?
def moveZeros(self,nums):
n = len(nums)
res = [ x for x in nums if x>0 ] for i in range(n-len(res)):
res.append(0) for i in xrange(len(nums)):
nums[i] = res[i] def moveZeros2(self,nums):
if nums == None or len(nums)==0:
return
j = 0
for i in xrange(len(nums)):
if nums[i]:
nums[i],nums[j] = nums[j],nums[i]
j += 1
Move Zeroes的更多相关文章
- LeetCode:Move Zeroes
LeetCode:Move Zeroes [问题再现] Given an array nums, write a function to move all 0's to the end of it w ...
- [LintCode] Move Zeroes 移动零
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- 【5_283】Move Zeroes
终于碰到一道水题,睡觉去~ Move Zeroes Total Accepted: 37369 Total Submissions: 88383 Difficulty: Easy Given an a ...
- Leetcode-283 Move Zeroes
#283. Move Zeroes Given an array nums, write a function to move all 0's to the end of it while mai ...
- 283. Move Zeroes(C++)
283. Move Zeroes Given an array nums, write a function to move all 0's to the end of it while mainta ...
- leetcode之旅(7)-Move Zeroes
Move Zeroes 题目描述: Given an array nums, write a function to move all 0's to the end of it while maint ...
- LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List
283. Move Zeroes var moveZeroes = function(nums) { var num1=0,num2=1; while(num1!=num2){ nums.forEac ...
- 【leetcode】283. Move Zeroes
problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...
- 【leetcode】Move Zeroes
Move Zeroes 题目: Given an array nums, write a function to move all 0‘s to the end of it while maintai ...
- 283. Move Zeroes【easy】
283. Move Zeroes[easy] Given an array nums, write a function to move all 0's to the end of it while ...
随机推荐
- CSS中:display:none与visible:hidden的区别
display:none视为不存在且不加载,即,不为被隐藏的对象保留其物理空间,即该对象在页面上彻底消失. visibility:hidden隐藏,但在浏览时保留位置,即,使对象在网页上不可见,但该对 ...
- 安装SQL Server驱动到Maven仓库[转]
from:http://raysen.blog.51cto.com/324335/1159232 Maven does not directly support some libraries, lik ...
- OpenJudge计算概论-找最大数序列
/*===================================== 找最大数序列 总时间限制: 1000ms 内存限制: 65536kB 描述 输入n行(n 不大于 30),每行不超过10 ...
- 【性能诊断】六、并发场景的性能分析(windbg案例,大量的内部异常造成CPU飙升)
在做产品的某个核心模块的性能优化时,发现压到100并发时应用服务器的CPU就飙升至90%以上,50并发以后TPS就基本定格在一个数值上.使用性能监视器收集应用服务器的数据,发现每秒的.NET CLR ...
- 数字根(digital root)
来源:LeetCode 258 Add Dights Question:Given a non-negative integer num , repeatedly add all its digi ...
- Linux启动过程详解 (转)
启动第一步--加载BIOS当你打开计算机电源,计算机会首先加载BIOS信息,BIOS信息是如此的重要,以至于计算机必须在最开始就找到它.这是因为BIOS中包含了CPU的相关信息.设备启动顺序信息.硬盘 ...
- nginx path_info问题解决
问题: 访问www.xxxx.com/index.php/api/xxxxxxxxx网址时,提示无法访问,找不到页面 解决: 第一次,是改了nginx.conf,不会报这个错误了,但还是没有用 loc ...
- HBase(七): HBase体系结构剖析(下)
目录: write Compaction splite read Write: 当客户端发起一个Put请求时,首先根据RowKey寻址,从hbase:meta表中查出该Put数据最终需要去的HRegi ...
- LintCode "Number of Islands II"
A typical Union-Find one. I'm using a kinda Union-Find solution here. Some boiler-plate code - yeah ...
- Java 提示 JRE unbound 或者 Tomcat unbound 解决方法
解决步骤 1.右键你的项目>>build path>>config build path 2. 选中显示 unbound的 Libraries >>Edit 如下图 ...