题目

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.

分析

给定一个整数数组,将数组中的所有0元素,移至序列末尾,且不能改变其它非零元素的原始顺序。

要求,空间复杂度为常量,不可使用辅助空间。

很简单的题目,只需遍历一次将非0元素从首位置依次赋值即可。

AC代码

class Solution {
public:
void moveZeroes(vector<int>& nums) {
if (nums.empty())
return; int sz = nums.size(); int count = 0;
for (int i = 0, k = 0; i < sz; ++i)
{
if (nums[i] != 0)
{
nums[count] = nums[i];
++count;
}
continue;
}
while (count < sz){
nums[count++] = 0;
}
return;
}
};

GitHub测试程序源码

LeetCode(283)Move Zeroes的更多相关文章

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

  2. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  3. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  4. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  5. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  6. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  7. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  8. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  9. LeetCode(4)Median of Two Sorted Arrays

    题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

随机推荐

  1. 【aspnetcore】抓取远程图片

    找到要抓取的图片地址:http://i.imgur.com/8S7OaEB.jpg 抓取的步骤: 请求图片路径 获取返回的数据 将数据转换为stream 将stream转换为Image 保存Image ...

  2. Net Core2-JWT

    NET Core2 http://www.cnblogs.com/wyt007/category/1130278.html JWT 设计解析及定制 前言 上一节我们讲述的书如何使用jwt token, ...

  3. (转)企业配置sudo命令用户行为日志审计

    原文:https://www.cnblogs.com/Csir/p/6403830.html?utm_source=itdadao&utm_medium=referral 第15章 企业配置s ...

  4. CentOS7.5 搭建mycat1.6.6

    1.环境及版本 操作系统:   CentOS 7.5 数据库:MySQL 5.7.23 jdk:1.8.0_191 mycat:1.6.6.1 cat /etc/centos-release mysq ...

  5. Home is where your heart is

    Home is where your heart is.心之所在即为家.

  6. CMSG_COMPAT_ALIGN函数

    CMSG_COMPAT_ALIGN函数是什么的使用方法?

  7. Jenkins系列——使用Dashboard View分类展示作业

    1.目标 创建的作业多了,在一个视图中展示多有不便.因此需要使用 Dashboard View 将作业按照后缀进行分类展示. 如下图,可以按照后缀添加CODE,TEST和OTHER视图. 2.环境说明 ...

  8. python2含有中文路径报错解决办法[\xe4\xbf\xa1\xe6\x81\xaf]

    如图所示 百度的解决办法大多数是针对python3版本的,在脚本开头加# -*- coding:utf-8 -*-,但是python2版本加了编码格式,还是报错,具体解决办法是:path =unico ...

  9. line-block,white-space,overflow

    line-block:设置行间的距离(行高),只能控制块级元素,span这样的行内元素无法控制,并且当块级元素 中包含span的时候设置line-block会使span的自适应高度小于块级元素的高度, ...

  10. HDU 5091 Beam Cannon (扫描线思想)

    题意:移动一个矩形,使矩形内包含的点尽量多. 思路:把一个点拆成两个事件,一个进(权值为1)一个出(权值为-1),将所有点按照x排序,然后扫描,对于每个x,用一个滑窗计算一下最大值,再移动扫描线.树状 ...