[LintCode] 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.
Notice
- You must do this in-place without making a copy of the array.
- Minimize the total number of operations.
Given nums = [0, 1, 0, 3, 12]
, after calling your function, nums
should be [1, 3, 12, 0, 0]
.
LeetCode上的原题,请参见我之前的博客Move Zeroes。
解法一:
class Solution {
public:
/**
* @param nums an integer array
* @return nothing, do this in-place
*/
void moveZeroes(vector<int>& nums) {
for (int i = , j = ; i < nums.size(); ++i) {
if (nums[i]) {
swap(nums[i], nums[j++]);
}
}
}
};
解法二:
class Solution {
public:
/**
* @param nums an integer array
* @return nothing, do this in-place
*/
void moveZeroes(vector<int>& nums) {
int left = , right = ;
while (right < nums.size()) {
if (nums[right]) {
swap(nums[left++], nums[right]);
}
++right;
}
}
};
[LintCode] Move Zeroes 移动零的更多相关文章
- [LeetCode] Move Zeroes 移动零
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- [LeetCode] 283. Move Zeroes 移动零
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- [LintCode] Trailing Zeroes 末尾零的个数
Write an algorithm which computes the number of trailing zeros in n factorial. Have you met this que ...
- [leetcode]283. Move Zeroes移零
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- 283. Move Zeroes把零放在最后面
[抄题]: Given an array nums, write a function to move all 0's to the end of it while maintaining the r ...
- 283 Move Zeroes 移动零
给定一个数组 nums, 编写一个函数将所有 0 移动到它的末尾,同时保持非零元素的相对顺序.例如, 定义 nums = [0, 1, 0, 3, 12],调用函数之后, nums 应为 [1, 3, ...
- 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】283. Move Zeroes
problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...
- LeetCode:Move Zeroes
LeetCode:Move Zeroes [问题再现] Given an array nums, write a function to move all 0's to the end of it w ...
随机推荐
- 顺序表C语言版
#include <stdio.h> /* * 顺序表最多输入N个数 */ #define N 10 #define OK 1 #define ERROR -1 struct sequeu ...
- Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA/(树链剖分+数据结构) + MST
E. Minimum spanning tree for each edge Connected undirected weighted graph without self-loops and ...
- Android studio导入eclipse项目且不改变目录结构
Android studio的安装与配置论坛当中已经有很多在此就不在细说了,现在开始说下如何在Android studio当中导入eclipse的项目且不改变其目录结构和配置,让使用eclipse的同 ...
- Linux内核装载和启动一个可执行程序
“平安的祝福 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ” 理解编 ...
- Eclipse中Ctrl+方法名发现无法进入到该方法中……
我现在的情况是,按住Ctrl点击该方法后,发现进入不到这个方法的定义. 后来我发现,是因为这个项目是在某个项目文件夹中,如下: 这时直接找到server这个项目就没有问题了,如图:
- 个人js类库mycool
// JavaScript Document Sunbye 1.0 //getElementById //function start var $=function(_id){return docum ...
- DELPHI与C#语法比较
1.我做了三年的.NET,也是三个月前因为项目需要转的delphi整个过渡差不多要一周到两周.正常情况两周后就能熟悉delphi.delphi可以调整开发环境的,你把他的属性和解决方案窗口调成和你用V ...
- 转:Delphi和Office程序开发 --不错可查阅
http://www.delphifans.com/infoview/Article_730.html日期:2006年2月20日 作者:潇潇2003 人气:5602 查看:[大字体 中字体 小字体] ...
- angular.bind() 函数
angular.bind bind 函数有三个参数, 参一:是一个对象 参二:是一个 function 参三:是用来给参二传参数的,可写可不写,看你心情 参数也可以在调用函数的时候传,也可以当做第三个 ...
- uva-1339Ancient Cipher
Ancient Roman empire had a strong government system with various departments, including a secret ser ...