描述

给定一个数组nums,写一个函数,将数组中所有的0挪到数组的末尾,维持其他所有非0元素的相对位置。 举例: nums = [0, 1, 0, 3, 12],

函数运行后结果为[1, 3, 12, 0, 0]

解析

快慢指针,慢指针指向第一个0,快指针指向第一个非0.

代码

public static void main(String[] args) {
int[] n = {4,2,4,0,0,3,0,5,1,0};
moveZero(n);
System.out.println(JSON.toJSONString(n));
} public static void moveZero(int[] n) {
if (n == null || n.length < 2) {
return;
}
int slow = 0;
int fast = 1;
while (fast < n.length && slow < n.length) {
if (n[slow] == 0 && n[fast] != 0) {
swap(n, slow++, fast++);
} else if (n[slow] == 0 && n[fast] == 0) {
fast++;
} else {
slow++;
fast++;
}
}
} public static void swap(int[] n, int a, int b) {
if (a == b) {
return;
}
n[a] = n[a] ^ n[b];
n[b] = n[a] ^ n[b];
n[a] = n[a] ^ n[b];
}

[LeetCode] 283. Move Zeroes ☆(移动0到最后)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. leetcode 283. Move Zeroes -easy

    题目链接:https://leetcode.com/problems/move-zeroes/ 题目内容: Given an array nums, write a function to move ...

  4. [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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. Linux——定时任务crontab

    linux内置的cron进程能帮我们实现这些需求,cron搭配shell脚本,非常复杂的指令也没有问题. cron介绍 我们经常使用的是crontab命令是cron table的简写,它是cron的配 ...

  2. (十)java虚拟机性能监控工具

    一. jps(Java Virtual Machine Process Status Tool) jps主要用来输出JVM中运行的进程状态信息.语法格式如下: 命令行参数选项说明如下: 1.1 案例 ...

  3. C# http请求 设置代理(标题可以作为搜索关键字)

    例一(C# 通过代理发HTTP请求): https://q.cnblogs.com/q/88682/ 例二(C# 代理HTTP请求): https://www.cnblogs.com/ShalenCh ...

  4. Can't create a new thread (errno 11) 解决办法 mysql无法连接

    问题的现象: 错误信息: ERROR 1135 (00000): Can't create a new thread (errno 11); if you are not out of availab ...

  5. 关于新小米盒子的Recovery模式如何进入

    26日下的盒子订单,经过几经波折,终于在昨天来到了我的面前,新东西到手,难免少不了一些折腾:升级系统,安装软件等.一顿折腾之后,想完全恢复到出厂设置,给盒子清一下,想进入盒子的Recovery模式,按 ...

  6. git 提交大小超过100M

    #MsnDialog.ad, #MyMoveAd, #QQ_Full, #ad-SNSSplashAd, #ad6cn, #adBody07, #adLeftFloat, #adRightFloat, ...

  7. 最新 美柚java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿. 美柚等10家互联网公司的校招Offer,因为某些自身原因最终选择了 美柚.6.7月主要是做系统复习.项目复盘.LeetCo ...

  8. 最新 创蓝253java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.创蓝253等10家互联网公司的校招Offer,因为某些自身原因最终选择了创蓝253.6.7月主要是做系统复习.项目复盘.Le ...

  9. Netty学习笔记(二)——netty组件及其用法

    1.Netty是 一个异步事件驱动的网络应用程序框架,用于快速开发可维护的高性能协议服务器和客户端. 原生NIO存在的问题 1) NIO的类库和API繁杂,使用麻烦:需要熟练掌握Selector.Se ...

  10. apache-phoenix-5.0.0-HBase-2.0-bin 登陆报错

    1.Error: org.apache.hadoop.hbase.DoNotRetryIOException: java.io.IOException: Unable to load configur ...