LeetCode OJ :Move Zeroes (移动0)
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就可以了
#include <vector>
using namespace std;
class Solution{
public:
void moveZeros(vector<int> & nums){
auto sz = nums.size();
int pos = ;
for (int i = ; i < sz; ++i){
if (nums[i] != )
nums[pos++] = nums[i];
}
for (int i = pos; i < sz; ++i)
nums[i] = ;
}
};
大体上就是这样
java版本代码如下:
public class Solution {
public void moveZeroes(int[] nums) {
int pos = 0;
for(int i = 0; i < nums.length; ++i){
if(nums[i] != 0){
nums[pos++] = nums[i];
}
}
while(pos < nums.length){
nums[pos++] = 0;
}
}
}
LeetCode OJ :Move Zeroes (移动0)的更多相关文章
- [LeetCode] 283. Move Zeroes ☆(移动0到最后)
描述 给定一个数组nums,写一个函数,将数组中所有的0挪到数组的末尾,维持其他所有非0元素的相对位置. 举例: nums = [0, 1, 0, 3, 12], 函数运行后结果为[1, 3, 12, ...
- 【leetcode】Move Zeroes
Move Zeroes 题目: Given an array nums, write a function to move all 0‘s to the end of it while maintai ...
- LN : leetcode 283 Move Zeroes
lc 283 Move Zeroes 283 Move Zeroes Given an array nums, write a function to move all 0's to the end ...
- 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 ...
- leetcode 283. Move Zeroes -easy
题目链接:https://leetcode.com/problems/move-zeroes/ 题目内容: Given an array nums, write a function to move ...
- [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 ...
- Java [Leetcode 283]Move Zeroes
题目描述: Given an array nums, write a function to move all 0's to the end of it while maintaining the r ...
- Leetcode 283 Move Zeroes python
题目: Given an array nums, write a function to move all 0's to the end of it while maintaining the rel ...
- 11. 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 ...
随机推荐
- DBCC SHRINKFILE收缩日志/收缩数据库/收缩文件
DBCC SHRINKFILE 收缩相关数据库的指定数据文件或日志文件大小. 语法 DBCC SHRINKFILE ( { file_name | file_id } { [ ,t ...
- 35个例子学会find
find的使用格式如下: $ find <指定目录> <指定条件> <指定动作> - <指定目录>: 所要搜索的目录及其所有子目录.默认为当前目录. - ...
- mysql-xtrabackup
使用xtrabackup进行MySQL数据库备份 2013年10月04日 ⁄ MySQL ⁄ 共 11306字 ⁄ 使用xtrabackup进行MySQL数据库备份已关闭评论 ⁄ 被围观 34,116 ...
- DP专题·三(01背包+完全背包)
1.hdu 2126 Buy the souvenirs 题意:给出若干个纪念品的价格,求在能购买的纪念品的数目最大的情况下的购买方案. 思路:01背包+记录方案. #include<iostr ...
- AOP-面向方面编程
面向方面编程(aspect-oriented programming,AOP)是一种编程范式,旨在提高模块化允许横向关注点的分离.该范式以一种成为方面(Aspect)的语言构造为基础,切面是一种新的模 ...
- python之路 面向对象基础 XML
一.面向对象基础 1.类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 类变量:类变量在整个实例化的对象中是公用的.类变量定义 ...
- PHP范例注册审核
<body> <h1>注册</h1> <form action="zcchuli.php" method="post" ...
- 通过自动回复机器人学Mybatis:OGNL+log4j.properties
imooc视频学习笔记 ----> URL:http://www.imooc.com/learn/154 OGNL规则: 从哪里取?(作用域.取值范围,例如封装入一个对象,该对象就是取值范围) ...
- MySQL-5.7 Update语句详解
1.语法 (1)单表 UPDATE [LOW_PRIORITY] [IGNORE] table_reference SET assignment_list [WHERE where_condition ...
- [POI2012] BEZ-Minimalist Security
一张n个点m条边的无向图,有点权有边权都是非负,且每条边的权值小于等于两个顶点的权值和,现在要将每个点减一个非负整数使得每条边权等于两个顶点的点权和,问最大修改代价和最小修改代价 思路神的一匹,完全想 ...