LeetCode(283)Move Zeroes
题目
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;
}
};
LeetCode(283)Move Zeroes的更多相关文章
- 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 ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- 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 ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- NET Core Web发布包
给ASP.NET Core Web发布包做减法 https://www.cnblogs.com/sheng-jie/p/9122582.html 1.引言 紧接上篇:ASP.NET Core Web ...
- (转)linux下od命令的使用
linux下od命令的使用 原文:http://blog.csdn.net/shylock_backer/article/details/46473283 名称:od作用:格式化输出文件中的数据提要: ...
- springboot使用schedule定时任务
定时任务一般会存在中大型企业级项目中,为了减少服务器.数据库的压力往往会采用时间段性的去完成某些业务逻辑.比较常见的就是金融服务系统推送回调,一般支付系统订单在没有收到成功的回调返回内容时会持续性的回 ...
- Linux下使用crontab命令配置定时任务
一.语法结构 crontab [-e [UserName]|-l [UserName]|-r [UserName]|-v [UserName]|File ] 说明 : crontab 是用来让使用者在 ...
- SQL Server一个特殊的阻塞案例分析2
最近发现一个非常奇怪的阻塞问题,如下截图所示(来自监控工具DPA),会话583被会话1036阻塞,而且阻塞发生在tempdb,被阻塞的SQL如下截图所示,会话等待类型为LCK_M_S 因为DPA工具不 ...
- 谷歌浏览器Chrome developer tool详细介绍
http://www.cr173.com/html/19114_4.html 第 4 页 js调试源码控制面板 5 源码控制面板(js调试) Javascript的调试,基本上是通过源码控制面板和命令 ...
- win7下一直试用Beyond Compare 4
找到目录C:\Users\用户名\AppData\Roaming\BeyondCompare,将这个目录删除,重启compare即可.
- Spark性能调优之道——解决Spark数据倾斜(Data Skew)的N种姿势
原文:http://blog.csdn.net/tanglizhe1105/article/details/51050974 背景 很多使用Spark的朋友很想知道rdd里的元素是怎么存储的,它们占用 ...
- [Tracking] KCF + KalmanFilter目标跟踪
基于KCF和MobileNet V2以及KalmanFilter的摄像头监测系统 简介 这是一次作业.Tracking这一块落后Detection很多年了,一般认为Detection做好了,那么只要能 ...
- 关于tomcat的classloader的一点想法
关于tomcat的classloader相关的帖子网上非常多,我觉得比较好的有: https://www.jianshu.com/p/d90e4430b0b9 https://blog.csdn.ne ...