LeetCode 283 Move Zeroes(移动全部的零元素)
翻译
给定一个数字数组。写一个方法将全部的“0”移动到数组尾部。同一时候保持其余非零元素的相对位置不变。
比如,给定nums = [0, 1, 0, 3, 12],在调用你的函数之后,nums应该变为[1, 3, 12, 0, 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.
分析
一開始我还以为是要给非零元素排序呢,后来细致一看仅仅是保持相对位置不变就好了。
那就easy非常多了呀。
0 1 0 3 12 (index = 0, current = 0)
1 0 0 3 12 (index = 1, current = 1)
1 0 0 3 12 (index = 1, current = 2)
1 3 0 0 12 (index = 2, current = 3)
1 3 12 0 0 (index = 3, current = 4)
按上面的步骤来,当前的数字是0的话不做操作。非零的话将其与第一个零互换位置。
其核心在于这个第一个零的位置是怎样变化的。即便一開始不是0也没关系,大不了让这个非零数和自己交换位置呗,比方说:
1 2 0 3 12 (index = 0, current = 0)
1 2 0 3 12 (index = 1, current = 1)
1 2 0 3 12 (index = 2, current = 2)
1 2 3 0 12 (index = 3, current = 3)
1 2 3 12 0 (index = 4, current = 4)
翻译成代码就是:
#include <iostream>
#include <vector>
using namespace std;
void moveZeroes(vector<int>& nums) {
for (int index = 0, current = 0; current < nums.size(); current++) {
if (nums[current] != 0)
swap(nums[index++], nums[current]);
}
}
int main() {
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(0);
v.push_back(3);
v.push_back(12);
moveZeroes(v);
for (auto i : v) {
cout << i << " ";
}
return 0;
}
代码
class Solution {
public:
void moveZeroes(vector<int>& nums) {
for (int index = 0, current = 0; current < nums.size(); current++) {
if (nums[current] != 0)
swap(nums[index++], nums[current]);
}
}
};
LeetCode 283 Move Zeroes(移动全部的零元素)的更多相关文章
- 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 re ...
- [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 ...
- [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 ...
随机推荐
- spring mvc poi excel
Util类 package com.common.util; public class ExportUtil { private XSSFWorkbook wb = null; private XSS ...
- mysql中ibatis的limit动态传参
param.put("pageNo",pageNo); param.put("pageSize",pageSize); sqlMap中的用法 limit $ ...
- UVa-1339-古老的密码
这题的话,我们可以把字符串序列里面的字母直接计数,然后比较两个数组里面的数字是否一一相同,然后就可以直接判定YES or NO. 因为它题目中说的就是一种映射的关系,首先我们读入之后,把两个字符串的不 ...
- Css选择器和JQuery基本编程接口
使用JQuery之前,首先从官网下载库文件 http://jquery.com/ jquery-2.1.4.js和jquery-2.1.4.min.js,前者是完整无压缩版本,用于开发调试:后者是压缩 ...
- 快速部署jumpserver堡垒机
jumpserver版本:Version 1.4.1-2 (社区版) 主机IP地址:10.0.0.105 准备环境1.安装依赖yum -y install wget sqlite-devel xz g ...
- Linux基础学习-命令行与图形界面切换
命令行模式和图形界面模式切换 打开文件 vim /etc/inittab # systemd uses 'targets' instead of runlevels. By default, ther ...
- 搭建Mysql主从复制
mysql 主从复制流程图 Server version: 10.0.24-MariaDB-7 Ubuntu 16.04 Master 记录二进制文件 导出数据并记录二进制位置 导入数据,设置二进制位 ...
- 「问题思考」python的递归中return返回none
代码: #求最大公约数 def gcd(x,y): if x < y: swap = x x = y y = swap if x%y == 0: return y else: gcd(y,x%y ...
- 【HDU 3336】Count the string(KMP+DP)
Problem Description It is well known that AekdyCoin is good at string problems as well as number the ...
- NowCoder小杰的签到题(模拟,思维)
链接: https://www.nowcoder.com/acm/contest/52/M 题意: 给定n个队伍的到场时间, 有3个报道位, 每个队伍报道需要b时间, 求所有报道完成的时间. 分析: ...