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. iOS语言本地化,中文显示

    尽管一直相信xcode肯定提供有语言本地化的设置地方,可是一直也没凑着去改.非常多的汉化,还是使用代码去控制:比方navagition的return使用代码改动为"返回"! 近期在 ...

  2. json对象和json字符串之间的转换-JavaScript实现

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  3. appium----基本概念

    转:http://www.cnblogs.com/nbkhic/p/3803830.html Client/Server Architecture appium的核心其实是一个暴露了一系列REST A ...

  4. eclipse / ADT(Android Develop Tool) 一些方便的初始设置

      1.设置编辑窗口的背景色eclipse的主编辑窗口的背景色,默认为白色,个人感觉太亮,推荐保护视力的“墨绿色”,当然也可以根据个人喜好更改,如下图 2.主编辑窗口的字体字号等,也可以根据自己的爱好 ...

  5. 人不在囧途 便携式3G上网设备+套餐推介

    来源: http://network.pconline.com.cn/317/3174920_all.html [PConline资讯]过年回家,本该是再高兴不过的事,可一想到要在路上颠簸数十个小时, ...

  6. 我最喜欢的模板jade(pug)学习和使用

    由于版权问题,现已改名pug.但无须担心,几乎没什么区别.就算依然使用jade也不会有太大影响. 慢慢迁移过渡即可 # 官网 https://pugjs.org # github https://gi ...

  7. 使用程序修改系统(IE)代理设置

    文章都是发布在github再转到这边的,这边格式可能会乱掉.博客地址:benqy.com 这是本人在做的一个前端开发调试工具(HttpMock),功能是web服务器+http日记+http代理(类似f ...

  8. 解决Linux上解压jdk报错gzip: stdin: not in gzip format

    最近在阿里上买了个服务器玩,需要安装jdk,在解压过程中遇到了一些问题,又是一番Google度娘,终于解决了.问题原因让我有点无奈…… 输入 #tar -xvf jdk-8u131-linux-x64 ...

  9. linux 给用户修改权限

    #添加一个用户 useradd xiaoming #设置密码 passwd xiaoming 回程 //设置密码就行了 #把用户修改成root权限 vi /etc/passwd #找到xiaoming ...

  10. RHEL7安装部署ZooKeeper

    转载请注明出处:jiq•钦's technical Blog - 季义钦 文章说明: 分布式注冊中心(链接)须要安装的组件包括两个部分: 1.注冊中心服务(Zookeeper) 2.站点(Tomcat ...