题意:

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

思路:

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

C++

 class Solution {
public:
void moveZeroes(vector<int>& nums) {
int idx=;
for(int i=; i<nums.size(); i++)
if(nums[i]!=)
nums[idx++]=nums[i];
while(idx<nums.size())
nums[idx++]=;
}
};

AC代码

python3

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

AC代码

 class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
cnt=nums.count(0)
for i in range(cnt):
nums.remove(0)
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. springboot 使用war包部署

    ps://www.cnblogs.com/jiaoyiping/p/4251718.html

  2. Sharepoint2013商务智能学习笔记之使用Current User Filter筛选Excel 数据(六)

    Sharepoint自带的filter可以和Excel Web Access互动,下面将制作一个Demo,使用Current User Filter根据当前登录用户自动筛选Excel. 第一步,用Ex ...

  3. mysql失效的几种情况

    1.如果查询条件中有or,即使查询的条件中带有索引也会失效,如果想使用or,又不想让索引失效,只能将or条件中的所有列都加上索引 2.like 查询一%开头用不上索引, 3.隐式转换会使索引失效 比如 ...

  4. python-django框架中使用七牛云

    1:注册七牛云账号 https://www.qiniu.com/ js文件 链接:https://pan.baidu.com/s/1BW1svHqEsXrrTNtRobKkpg  提取码:ixta 2 ...

  5. JSP读取Oracle数据库里的图片Blob字段并显示在页面上

    1.java代码: /** * 打印模板获取电子签名 * @param request * @param resp * @param id * @return * @throws Exception ...

  6. ThinkSNS+ PHP开发概述

    Plus (读音:[plʌs],全称:ThinkSNS+ [θɪŋk es en es plʌs],是 ThinkSNS 系列产品一个重要版本,其软件识别名称为 Plus 即 +) 是一个基于 Lat ...

  7. Kubernetes基本概念之Name和NameSpace

    在Kubernetes中,所有对象都会被指定一个唯一的Name和UID. 用户还可以指定一些不要求唯一性的数据附加到对象上,例如Label和Annotation. 1. Name Name是创建一个K ...

  8. js抽奖系统

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. CSS十一问——好奇心+刨根问底=CSSer

    最近有时间,想把酝酿的几篇博客都写出来,今天前端小学生带着10个问题,跟大家分享一下学习CSS的一些体会,我觉得想学好CSS,必须保持一颗好奇心和刨根问底的劲头,而不是复制粘贴,得过且过.本人能力有限 ...

  10. linux 文件权限除了r、w、x外还有s、t、i、a权限说明

    linux 文件权限除了r.w.x外还有s.t.i.a权限 s: 文件属主和组设置SUID和GUID,文件在被设置了s权限后将以root身份执行.在设置s权限时文件属主.属组必须先设置相应的x权限,否 ...