题意:

  给定一个整型数组nums,要求将其中所有的0移动到末尾,并维护所有非0整数的相对位置不变。

思路:

  扫一遍,两个指针维护0与非0的交界,将非0的数向前赋值就行了。

C++

  1. class Solution {
  2. public:
  3. void moveZeroes(vector<int>& nums) {
  4. int idx=;
  5. for(int i=; i<nums.size(); i++)
  6. if(nums[i]!=)
  7. nums[idx++]=nums[i];
  8. while(idx<nums.size())
  9. nums[idx++]=;
  10. }
  11. };

AC代码

python3

  1. class Solution(object):
  2. def moveZeroes(self, nums):
  3. """
  4. :type nums: List[int]
  5. :rtype: void Do not return anything, modify nums in-place instead.
  6. """
  7. nums.sort(key=lambda x:0 if x==0 else -1 )

AC代码

  1. class Solution(object):
  2. def moveZeroes(self, nums):
  3. """
  4. :type nums: List[int]
  5. :rtype: void Do not return anything, modify nums in-place instead.
  6. """
  7. cnt=nums.count(0)
  8. for i in range(cnt):
  9. nums.remove(0)
  10. nums.append(0)

AC代码

LeetCode Move Zeroes (简单题)的更多相关文章

  1. LeetCode:Move Zeroes

    LeetCode:Move Zeroes [问题再现] Given an array nums, write a function to move all 0's to the end of it w ...

  2. [LeetCode] Move Zeroes 移动零

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  3. LeetCode——Move Zeroes

    Description: Given an array nums, write a function to move all 0's to the end of it while maintainin ...

  4. 力扣485. 最大连续1的个数-C语言实现-简单题

    题目 [题目传送门] 给定一个二进制数组, 计算其中最大连续1的个数. 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3 ...

  5. 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 ...

  6. 这样leetcode简单题都更完了

    这样leetcode简单题都更完了,作为水题王的我开始要更新leetcode中等题和难题了,有些挖了很久的坑也将在在这个阶段一一揭晓,接下来的算法性更强,我就要开始分专题更新题目,而不是再以我的A题顺 ...

  7. 【leetcode】Move Zeroes

    Move Zeroes 题目: Given an array nums, write a function to move all 0‘s to the end of it while maintai ...

  8. leetcode:283. Move Zeroes(Java)解答

    转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50493772 题目地址:https://leet ...

  9. leetcode简单题6

    今天的华师 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...

随机推荐

  1. 20169201 使用Metaspoit攻击MS08-067实验

    MS08-067漏洞介绍 MS08-067漏洞的全称为"Windows Server服务RPC请求缓冲区溢出漏洞",如果用户在受影响的系统上收到特制的 RPC 请求,则该漏洞可能允 ...

  2. [Windows]获取当前时间(年/月/日/时/分/秒)

    struct tm* GetCurTime(time_t inTime) { struct tm* curTime = localtime(&inTime); curTime->tm_y ...

  3. 2018牛客多校第九场E(动态规划,思维,取模)

    #include<bits/stdc++.h>using namespace std;const long long mod=1000000007,inv=570000004;long l ...

  4. 51nod1305(简单逻辑)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1305 题意:中文题诶- 思路:1e5的数据直接暴力肯定是不行 ...

  5. Topoi 测验1301, 问题C: 1959: 解题 解题报告

    Topoi(一个经常会炸的网站) 本题提交链接 很久以前的题目了, 刚开了博客,来写一波题解 先看一波提交记录: 调了好几天QAQ 唉! 要是这些高手里有我估计直接 输出1 就AC了 算法 DFS + ...

  6. jmeter 签名MD5生成(转)

    请求接口需要同时发送签名,签名定义为: 可以看出签名就是把用户的密码 .用户名 和签名key生成一个md5串就可以了 刚好jmeter 有个md5 生成,生成前需要获取name ,password k ...

  7. Decorator模式(装饰器模式)

    Decorator模式? 假如现在有一块蛋糕,如果只涂上奶油,其他什么都不加,就是奶油蛋糕.如果加上草莓,就是草莓奶油蛋糕.如果再加上一块黑色巧克力板,上面用白色巧克力写上姓名,然后插上代表年龄的蜡烛 ...

  8. D. Time to Raid Cowavans 分块暴力,感觉关键在dp

    http://codeforces.com/contest/103/problem/D 对于b大于 sqrt(n)的,暴力处理的话,那么算出每个的复杂度是sqrt(n),就是把n分成了sqrt(n)段 ...

  9. Python2.7编程基础(博主推荐)

    不多说,直接上干货! 见 http://www.runoob.com/python/python-tutorial.html

  10. 物体检测丨浅析One stage detector「YOLOv1、v2、v3、SSD」

    引言 之前做object detection用到的都是two stage,one stage如YOLO.SSD很少接触,这里开一篇blog简单回顾该系列的发展.很抱歉,我本人只能是蜻蜓点水,很多细节也 ...