描述

给定一个数组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. ubuntu18.04开机提示welcome to emergency mode! after logging in type...的解决办法

    开机就是报错,进不了系统. 原因是我的ubuntu电脑绑定了之前的移动硬盘,而我开机的时候并没有插着移动硬盘. 所以解决办法是,在此命令行下,以root用户的身份(我这里默认是root用户),vim ...

  2. MySQL创建触发器样例

    # init DROP TABLE IF EXISTS students; DROP TABLE IF EXISTS class; # 创建测试用的班级表 CREATE TABLE class ( c ...

  3. Jquery异步请求数据实例代码

    一.Jquery向aspx页面请求数据 前台页面JS代码: 代码如下: $("#Button1").bind("click", function () { $. ...

  4. Paper Mark2

    论文:CBAM: Convolutional Block Attention Module 论文链接 pytorch代码 论文:Approach for Fashion Style Recogniti ...

  5. c/c++编码规范(3)--google代码规范检测工具cpplint.py

    cpplint.py是来自google开源项目风格错误检测工具.它是一个python脚本,和google开源项目风格指南一同发布.下载地址:https://github.com/google/styl ...

  6. 【c# 学习笔记】封装

    封装 指的是把类内部的数据隐藏起来,不让对象实例直接对其操作.c#中提供了属性机制来对类内部的状态进行操作. 在c#中,封装可以通过Public.Private.Protected和Internal等 ...

  7. Halcon 学习2 金属雕刻字识别

    *HALCON 里面的例程名称:engraved_cnn.hdevread_image (Image, 'engraved')get_image_size (Image, Width, Height) ...

  8. input标签自动填充问题

    <input type='text' placeholder='手机号' /> <input type='text' placeholder='地址' /> <input ...

  9. 1、4 前后端分离,写静态HTML文件,通过ajax 返回数据

    1.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti ...

  10. springboot集成elk 四:springboot + Elasticsearch+Jest

    依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spri ...