题目

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. PT100/PT1000

    热敏电阻:互换性差,非线性严重,测量范围窄-50~300℃. 金属电阻:准备稳定可靠.-200~500℃ PT100:测量范围宽比PT1000宽,分辨率比PT1000低(100倍,即PT1000每变化 ...

  2. Zepto事件模块源码分析

    Zepto事件模块源码分析 一.保存事件数据的handlers 我们知道js原生api中要移除事件,需要传入绑定时的回调函数.而Zepto则可以不传入回调函数,直接移除对应类型的所有事件.原因就在于Z ...

  3. 3.Freshman阶段学习内容的确定

    我刷知乎.在知乎上答题的程序员,不是很牛逼就是更牛逼,说起各种系统.各种系统的各种版本.各种语言.数据库.算法.IT届的各种圣战都有板有眼.信手拈来.头头是道,不得不服.这导致了一些非常严重的问题:我 ...

  4. css简单动画

    这几天公司需要更新一个移动端web的页面,因为任务简单,就交给作为菜鸟新人的我来做.第一次接触css还是在14年刚上大一的时候跟着html一起学习的,之后就再也没有接触过.所以只好一边学习,一边完成任 ...

  5. Ubuntu 16.04 server版本开机启动脚本不支持

    Ubuntu16.04开机启动的脚本一直不支持,错误用在将开机启动脚本放到了home/usr/的目录下,应该放到/root才能正常启动.#!/bin/sh -e ## rc.local## This ...

  6. iOS开发 - Protocol协议及委托代理(Delegate)

    因为Object-C是不支持多继承的,所以很多时候都是用Protocol(协议)来代替.Protocol(协议)只能定义公用的一套接口,但不能提供具体的实现方法.也就是说,它只告诉你要做什么,但具体怎 ...

  7. excel如何显示多个独立窗口

    https://blog.csdn.net/tigaobansongjiahuan8/article/details/76861084

  8. socket tcp使用recv接收数据时,返回errno错误代码88

    原因:就是recv函数的第一个参数不是可用的,也就是第一个参数不是建立连接时返回的文件描述符. 解决方法:xxx

  9. Servlet详解之两个init方法的作用

    在Servlet中 javax.servlet.GenericServlet类 继承自java.lang.Object 实现了Serializable,,servlet ,ServletConfig ...

  10. 香港城市大学:全球首创3D打印微型机器人技术 有望作治疗癌症用途

    香港城市大学(香港城大)的研究团队开发出了全球首创以磁力控制的3D打印微型机器人,该微型机器人技术能做到在生物体内精准运载细胞到指定的位置.新研发的微型机器人有望应用在治疗癌症的靶向治疗,并为细胞层面 ...