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 ... 
随机推荐
- MySQL之索引详解
			这篇博客将要阐述为什么使用b+树作为索引,而不是b树或者其他树 1.什么是b树 (图片来自网络) b树相关特性:⑴关键字分布在整棵树中 ⑵任何一个关键字只出现在一个节点上 ⑶搜索可能在非叶子节点上结束 ... 
- 新事物学习---Chrome上使用PWA
			PWA是什么 PWA(Progressive Web Apps)是 Google 最近在提的一种 Web App 形态 (或者如 Wikipedia 所称的"软件开发方法").PW ... 
- 2018.3.29 div格式设置
			<!DOCTYPE html><html> <head> <meta charset="UTF-8"> ... 
- Rails 定时任务——whenever实现周期性任务
			根据项目的进展,我们需要实现后台进行定时读取信息的功能,而最关键的实现部分是周期性功能,根据调研,决定使用whenever来实现这一功能. github:https://github.com/java ... 
- 软件工程第三次作业-结对作业NO.1
			第一次结对作业 结对人员: 潘伟靖 170320077 张 松 170320079 方案分析 我们对所供的资料进行分析,如下: 从提供的资料可以看出,需要解决的问题以及满足的需求主要有两类目标用户,各 ... 
- 20145237 实验五《Java网络编程》
			20145237 实验五<Java网络编程> 一.实验内容 •1.运行下载的TCP代码,结对进行,一人服务器,一人客户端: •2.利用加解密代码包,编译运行代码,一人加密,一人解密: •3 ... 
- 【iOS】Swift   if let 和 if var
			if let unwrappedOptional = postDict { print("The optional has a value! It's \(unwrappedOptional ... 
- Centos7 Yum方式安装Mysql7
			不废话,直奔主题,可以覆盖安装. 下载并安装MySQL官方的 Yum Repository [root@localhost ~]# wget -i -c http://dev.mysql.com/ge ... 
- python使用tesseract-ocr完成验证码识别(模型训练和使用部分)
			一.Tesseract训练 大体流程为:安装jTessBoxEditor -> 获取样本文件 -> Merge样本文件 –> 生成BOX文件 -> 定义字符配置文件 -> ... 
- c# windows service 实现监控其他程序是否被关闭,关闭则报警
			namespace MonitorService { public partial class MonitorSv : ServiceBase { string AppName = "&qu ... 
