leetcode 283. Move Zeroes -easy
题目链接:https://leetcode.com/problems/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.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
题目翻译:
给定一个数组,写一个函数,将数组中的0都移动到数组末尾,同时保持非零数字的顺序。
比如,数组nums=[0, 1, 0, 3, 12],调用你的函数后,数组变成[1, 3, 12, 0, 0]。
注意:
1. 你必须在原数组上进行修改,不能拷贝一个新数组;
2. 尽量减少你的操作次数。
设置两个指针fast, slow。快指针fast遍历,遇到非零数传回慢指针(slow)。如果快慢指针指向的不是同一个元素,则快指针指向的元素归零,然后慢指针向后移动一个格,
class Solution {
public:
void moveZeroes(vector<int>& nums) {
for(int fast=0,slow=0;fast<nums.size();fast++){ //快指针fast遍历,遇到非零数传回慢指针(slow)。如果快慢指针指向的不是同一个元素,则快指针指向的元素归零,然后慢指针向后移动一个格,
if(nums[fast]!=0){
if(slow!=fast){ //
nums[slow]=nums[fast];
nums[fast]=0;
}
slow++;
}
}
}
};
leetcode 283. Move Zeroes -easy的更多相关文章
- 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(移动全部的零元素)
翻译 给定一个数字数组.写一个方法将全部的"0"移动到数组尾部.同一时候保持其余非零元素的相对位置不变. 比如,给定nums = [0, 1, 0, 3, 12],在调用你的函数之 ...
- [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 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 ...
随机推荐
- [poj3984]迷宫问题_bfs
迷宫问题 题目大意:给你一个5*5的矩阵,求左上角到左下角的最短路径. 注释:0或1的矩阵,1表示不能走,0表示能走,保证有唯一最短路径. 想法:bfs爆搜练习题.通过其实点,定义方向数组,然后进行b ...
- JVM学习八:常用JVM配置参数
前面学习的都是和类加载相关的知识,接下来学习的则和GC相关的知识,都是JVM的几个重点块. 零.在IDE的后台打印GC日志: 既然学习JVM,阅读GC日志是处理Java虚拟机内存问题的基础技能,它只是 ...
- DB2开发系列之四——触发器
1.触发器类型 1)BEFORE 触发器:在对表插入或更新之前执行该触发器,允许使用CALL 和 SIGNAL SQL 语句: 2)BEFORE DELETE 触发器:在删除操作之前执行该触发器: 3 ...
- Java读取word中表格
因为要新建一个站,公司要把word表格的部分行列存到数据库中.之前用java操作过excel,本来打算用java从word表格中读取数据,再存到数据库中,结果因为权限不够,无法访问公司要写的那个数据库 ...
- Access数据库自动生成设计文档
在做Access数据库设计时,常常直接在access文件中建表,建字段,然后写设计文档时,又得重新再写一遍字段和表间关系.其实access数据库自己就支持自动生成数据库文档. 操作方法如下: 数据库工 ...
- C语言第三次作业---单层循环结构
一.PTA实验作业 题目一.最佳情侣身高差 1.实验代码 int N;//存放输入的人数 char sex; double hight1,hight2;//分别存放输入的身高和输出的身高 scanf( ...
- 20155306 2006-2007-2 《Java程序设计》第3周学习总结
20155306 2006-2007-2 <Java程序设计>第3周学习总结 教材学习内容总结 第四章 认识对象 4.1 类与对象 定义类 1.先在程序中定义类: Clothes{ Str ...
- 20162302 实验三《敏捷开发与XP实践》实验报告
实 验 报 告 课程:程序设计与数据结构 姓名:杨京典 班级:1623 学号:20162302 实验名称:敏捷开发与XP实践 实验器材:装有IdeaU的联想拯救者80RQ 实验目的与要求:1.代码的格 ...
- 第四十六条:for-each循环优先于传统的for循环
for(Elements e : list) { //doSomeThing-- }
- web信息泄露注意事项
1. 确保您的Web服务器不发送显示有关后端技术类型或版本信息的响应头. 2. 确保服务器打开的端口上运行的所有服务都不会显示有关其构建和版本的信息. 3. 确保所有目录的访问权限正确,保证不会让攻击 ...