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 ...
随机推荐
- JavasJavaScript笔试题
这几天参加了好多场宣讲会,记录一下遇到的编程题还有平时练习遇到的(如果有更好的方法欢迎提出,本人也是菜鸟一枚): 1.数组查重,并返回出重复次数最多的项并记录重复次数: function chacho ...
- 线段树 & 题目
首先说下我写的线段树吧. 我是按照线段树[完全版]那个人的写法来写的,因为网上大多数题解都是按照他的写法来写. 确实比较飘逸,所以就借用了. 节点大小是maxn是4倍,准确来说是大于maxn的2^x次 ...
- SSIS-Dtsx包文件打开时一直验证
把每个项的DelayValidation设置为true. 也可以直接改文件: 把文件里的 <DTS:Property DTS:Name="DelayValidation"&g ...
- dede用户登录时,跳转到提示页时报404错误
做了一个项目,本地运行,用的是Apache服务器,一切正常. 可是当我把项目放到VPS中运行时,每当输入用户名登录时,调转到"成功登录,3秒钟后转向网站主页"的提示页面时,页面的顶 ...
- Error occurred while loading plugins. CLI functionality may be limited.
npm install --save-dev --save-exact @ionic/cli-plugin-ionic-angular@latest @ionic/cli-plugin-cordova ...
- ListView 视图(View)
自定义视图,设置默认ListView,ListViewItems默认样式 public class VirtualStackPanelView : ViewBase { public static r ...
- winform代码生成器(一)
(PS sqlhelper的文件 竟放到 类库里了,第二篇已做了分离,边做边写的 ^_^) 做 Winform 项目时,要拖很多控件,感觉在做重复的事,那就应该用程序来完成,那就自己写一个吧.-- ...
- django之基于cookie和装饰器实现用户认证
示例1 # Create your views here. user = "a" pwd = "a" def login(request): if reques ...
- 解决Mysql导入大数据出现gone away的问题
在用Mysql Yog或者PHPMyadmin等工具导入数据量大的sql文件时,会提示“gone away”,那么如何处理这个问题尼? 在Mysql对应的配置文件中my.ini文件中加入以下配置: # ...
- Android商城开发系列(六)——使用 OkHttpUtils 请求网络 + 使用 fastjson解析数据
OkHttp是Google推荐使用的一个开源的网络请求框架,Android开发中涉及到网络请求和接口调用现在大部分都是使用OkHttp,网上已经有不少人针对OkHttp进行了封装,这里推荐一下鸿洋大神 ...