leetcode-easy-array-283 move zeros
mycode 77.24%
class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: None Do not return anything, modify nums in-place instead.
"""
pos = 0
for i in range(len(nums)):
if nums[i] != 0 :
nums[pos] = nums[i]
pos += 1
nums[pos:] = [0]*len(nums[pos:])
参考:
思路类似于
26-Remove Duplicates from Sorted Array
def moveZeros(nums):
j = 0 # 记录非零元素应该换到第几个位置
for i in range(len(nums)):
if nums[i] != 0:
nums[j], nums[i] = nums[i], nums[j]
j += 1
return nums
print(moveZeros([1,0,1,0,3,12]))
leetcode-easy-array-283 move zeros的更多相关文章
- 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 Zeros; 27 Remove Elements; 26 Remove Duplicated from Sorted Array;
,,,,}; //把数组的值赋给vector vector<int> vec(arr, arr+sizeof(arr)/sizeof(int)); 解法一: 时间复杂度O(n) 空间复杂度 ...
- 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&Python] Problem 283. Move Zeroes
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- [Array]283. Move Zeroes
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- [刷题] 283 Move Zeros
要求 将所有的0,移动到vector的后面比如; [1,3,0,12,5] -> [1,3,12,5,0] 实现 第一版程序,时间.空间复杂度都是O(n) 1 #include<iostr ...
- 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 ...
- leetcode:283. Move Zeroes(Java)解答
转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50493772 题目地址:https://leet ...
- LN : leetcode 283 Move Zeroes
lc 283 Move Zeroes 283 Move Zeroes Given an array nums, write a function to move all 0's to the end ...
- 【leetcode】283. Move Zeroes
problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...
随机推荐
- 安装linux mint后要做20件事
Linux Mint 17 Qiana Cinnamon Linux Mint 17已经发布,定名为Qiana.Mint是Linux最佳发行版之一,它定位于桌面用户,关注可用性和简洁.它携带了风格迥异 ...
- vue动态渲染图片,引用路径需要注意的地方
1.把图片放在和src同级的static里面,这用按照正常的方式进行引入,例如: 2.图片可以在其他文件夹,但是在script引入是必须加上require <img :src="ite ...
- 生产服务器上安装Python
2018-05-17 生产环境的服务器(以下简称内网服务器)由于安全限制,可能无法连接外网.这种情况下将无法直接使用pip命令安装python的包 一.更改pip源 - 默认pip是使用Python官 ...
- PHP WEB 引擎缓存加速优化
PHP 缓存加速器介绍 操作码缓存 请求一个 PHP 程序时,PHP 引擎会解析程序,并且将编译码作为特定操作码.这是要执行的代 码的一种二进制表示形式.随后,此操作码有 PHP 引擎执行并丢弃.操作 ...
- Zabbix监控Dell服务器相关硬件资源
一.安装dell服务器硬件监控工具OMSA 1.安装dell的yum源 [root@push-- ~]# wget -q -O - http://linux.dell.com/repo/hardwar ...
- 递归算法几个实例---C/C++
//1.斐波那契数列 int fibo(int n) { || n==) { ; } else { ) + fibo(n-); } } //2.阶乘 int fac(int n) { || n==) ...
- VxWorks引导启动过程
https://blog.csdn.net/phunxm/article/details/6979089
- CodeForces-721D-Maxim and Array(优先队列,贪心,分类讨论)
链接: https://vjudge.net/problem/CodeForces-721D 题意: Recently Maxim has found an array of n integers, ...
- get和post请求的区别?
①get请求用来从服务器上获得资源,而post是用来向服务器提交数据: ②get将表单中数据按照name=value的形式,添加到action 所指向的URL 后面,并且两者使用“?”连接,而各个变量 ...
- spark的accumulator值保存在哪里?
答案:保存在driver端.因此需要对收集的信息的规模要加以控制,不宜过大.避免 driver端的outofmemory问题!!!