LeetCode:移动零【283】

题目描述

给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。

示例:

输入: [0,1,0,3,12]
输出: [1,3,12,0,0]

说明:

  1. 必须在原数组上操作,不能拷贝额外的数组。
  2. 尽量减少操作次数。

题目分析

   黑色指针表示循环过程,即从头到尾。蓝色指针表示要填的第N个数

   黑色指针一直走,遇到非0的就将它填到N位置(初始为0,每次填写后递增)。然后如果黑色指针到达末尾,蓝色指针后面的都变更为0.

  

Java题解

public class Solution {
public void moveZeroes(int[] nums) {
int index=0;
for(int i=0;i<nums.length;i++)
{
if(nums[i]!=0)
nums[index++]=nums[i];
}
while(index<nums.length)
nums[index++]=0;
}
}

LeetCode:移动零【283】的更多相关文章

  1. LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List

    283. Move Zeroes var moveZeroes = function(nums) { var num1=0,num2=1; while(num1!=num2){ nums.forEac ...

  2. LeetCode Array Easy 283. Move Zeroes

    Description Given an array nums, write a function to move all 0's to the end of it while maintaining ...

  3. 【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters

    题目 Given a string, find the length of the longest substring without repeating characters. For exampl ...

  4. 【LeetCode从零单排】No189 .Rotate Array

    称号 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the arr ...

  5. 【LeetCode从零单排】No15 3Sum

    称号 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...

  6. LeetCode —— 移动零

    给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数组上操作, ...

  7. [LeetCode&Python] Problem 283. Move Zeroes

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  8. leetcode 移动零

    给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 这道题比较好想出来 /** ...

  9. 【LeetCode从零单排】No.135Candy(双向动态规划)

    1.题目 There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  10. 【LeetCode从零单排】No 114 Flatten Binary Tree to Linked List

    题目 Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 ...

随机推荐

  1. react-native 项目实战 -- 新闻客户端(1) -- 初始化项目结构

    1.在项目根目录新建Componet文件夹(专门用来放我们的自定义组件),里面新建Main.js.Home.js.Message.js.Mine.js.Find.js 2.修改 index.andro ...

  2. kettle入门(七) 之kettle增量方案(一)全量比对取增量-依据唯一标示

    引: ods有个project表来自于上游系统,数据量不大 十几万,下游系统须要此数据,而且须要每天提供截止当天的增量数据 要求每条数据给出数据变化时间及标示,即数据若是插入 有插入时间和插入标示 若 ...

  3. centos安装postgresql

    #安装postgresqlyum -y install postgresql-server #执行数据库初始化脚本service postgresql-9.2 initdb #启动服务service ...

  4. 再议urlconnection和socket区别

    利用URL进行通信与利用socket进行通信有许多相似之处.它们都是利用建立连接.获取流来进行通信.那么,它们的区别在何处呢?    利用socket进行通信时,在服务器端运行一个socket通信程序 ...

  5. Android中常用布局单位

    Android在UI布局时经常用到一些单位,对单位混用直接会影响UI的显示,要想正确的在布局中使用每种单位就必须先真正的熟悉它. UI显示效果的影响因素:屏幕尺寸.屏幕密度.分辨率:而android手 ...

  6. jvm性能调优常用命令

    说明和名词解释: ①  只有进行的运行用户才可以调用命令查看相关信息 ②  [pid] 为需要查看的进程的端口号 ③  [file] 为需要导出到的文件的具体地址 ④ [tid] 进程中线程的id 1 ...

  7. HDU 1014 Uniform Generator 题解

    找到规律之后本题就是水题了.只是找规律也不太easy的.证明这个规律成立更加不easy. 本题就是求step和mod假设GCD(最大公约数位1)那么就是Good Choice,否则为Bad Choic ...

  8. Hbase和RDBMS(关系数据库管理系统)区别

    hbase是一个基于列模式的映射数据库,键--->数据 的映射,大大简化了传统数据   数据类型:hbase的存储的数据都是字符串,所有的类型都有用户自己处理,他只保存字符串;传统的数据有丰富的 ...

  9. 【数据挖掘】分类之Naïve Bayes(转载)

    [数据挖掘]分类之Naïve Bayes 1.算法简介 朴素贝叶斯(Naive Bayes)是监督学习的一种常用算法,易于实现,没有迭代,并有坚实的数学理论(即贝叶斯定理)作为支撑. 本文以拼写检查作 ...

  10. LeetCode 206. Reverse Linked List(迭代和递归两种实现)

    递归的代码比迭代的代码看起来更清爽一些,也是由于递归对行为进行了抽象吧. 注意到,这是一个尾递归函数.一些编译器会将它优化为迭代,这样一方面,在代码层面保持了清晰的逻辑和可读性.一方面保持了代码的性能 ...