描述

给定一个数组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. MySQL数据库表的设计和优化(下)

    二.基于单表设计的多表设计原则:(1)表关系: 一)一对一关系: 定义: 在这种关系中,关系表的每一边都只能存在一个记录.每个数据表中的关键字在对应的关系表中只能存在一个记录或者没有对应的记录.这种关 ...

  2. 微信小程序之对象转化为数组

    对象转成数组方式一(数组里面是一个个number类型的元素) let dictObject= { , , , , }; // 对象转成数组方式一 var createArr = [] for (let ...

  3. sudo内容

    [root@bogon ~]# cat /etc/sudoers## Sudoers allows particular users to run various commands as## the ...

  4. iOS从App跳转至系统设置菜单各功能项

    跳到系统设置里的WiFi界面 info里面设置: 在项目中的info.plist中添加 URL types 并设置一项URL Schemes为prefs,如下图 代码: 复制代码 代码如下: NSUR ...

  5. iOS-UIView的layoutSubviews和drawRect方法何时调用(转)

    转自:http://jianyu996.blog.163.com/blog/static/112114555201305113018814/ 首先两个方法都是异步执行.layoutSubviews方便 ...

  6. 如何去除PATH里的重复项并排序

    注意sed的用法,linux和Mac os不同,linux是Gnu的,Mac是BSD的 PATH排序去掉重复内容 mac和linux的换行符替换方法不一样,如下是Mac下的操作 export PATH ...

  7. Navicat安装及使用

    一.安装Navicat 1.下载安装文件:navicat11.0.17_premium_cs_x86.exe(32位) 2.Oracle 的 Instance Client:instantclient ...

  8. Sping 补充完成修改功能

    1.视图层完整示例 <form action="#" th:action="@{/update/{id}(id=${user.id})}" th:obje ...

  9. Rhino脚本引擎技术介绍

    引用:http://p.primeton.com/articles/54c1e255be20aa4735000001 http://blog.csdn.net/u013292493/article/d ...

  10. javascript jssdk退出微信的方法

    javascript jssdk退出微信的方法 <pre> $('.tctip').on('click',function () { setTimeout("WeixinJSBr ...