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 ...
随机推荐
- MySQL水平分表
业务表增长速度较快,单表数据较大,对表的读写有影响. 思路:化整为零,把单表拆解为多表,按指定的算法规则选择表. 好处:能大幅降低单表的数据,读写更快,同时分散了表数据, SQL语句也分散到不同的表中 ...
- vue-cli3.0中使用 postcss-pxtorem
vue.config.js module.exports = { lintOnSave: true, css: { loaderOptions: { postcss: { plugins: [ req ...
- 用 C++ 模板元编程实现有限的静态 introspection
C++ 中的奇技淫巧大部分来源于模板技术,尤其是模版元编程技术(Template Meta-Programming, TMP).TMP 通过将一部分计算任务放在编译时完成,不仅提高了程序的性能,还能让 ...
- linux centos中安装flash player
本机为centos 7.0 64 1.用火狐浏览器随便打开一个视频网站,找到一个视频点进去,网站会提示未安装flash player.点击进去到adobe官网.2.下载flash player插件,可 ...
- CSS布局方式
1.内边距 padding <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...
- Apache 80跳转443
<VirtualHost *:> ServerName your.domain.com #域名 RewriteEngine on #启用重定向 RewriteCond %{SERVER_P ...
- C#.net中的rank方法
string[,] abcd = new string[2, 4];abcd[0, 0] = "a";abcd[0, 1] = "b";abcd[0, 2] = ...
- SugarCRM开发入门
SugarCRM官网下载地址:https://sourceforge.net/projects/sugarcrm/ 概述 Sugar最初是基于LAMP(Linux.Apache.MySQL和PHP)运 ...
- 使用纯注解方式实现账户的CRUD
1 需求和技术要求 1.1 需求 实现账户的CRUD. 1.2 技术要求 使用Spring的IOC实现对象的管理. 使用QueryRunner作为持久层的解决方案. 使用C3p0作为数据源. 2 搭建 ...
- Java中CharSet字符集
java.nio.charset包中提供了Charset类,它继承了Comparable接口:还有CharsetDecoder.CharsetEncoder编码和解码的类,它们都是继承Object类. ...