[LeetCode] 283. Move Zeroes ☆(移动0到最后)
描述
给定一个数组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到最后)的更多相关文章
- 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 ...
- 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 ...
- leetcode 283. Move Zeroes -easy
题目链接:https://leetcode.com/problems/move-zeroes/ 题目内容: Given an array nums, write a function to move ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- ubuntu18.04开机提示welcome to emergency mode! after logging in type...的解决办法
开机就是报错,进不了系统. 原因是我的ubuntu电脑绑定了之前的移动硬盘,而我开机的时候并没有插着移动硬盘. 所以解决办法是,在此命令行下,以root用户的身份(我这里默认是root用户),vim ...
- MySQL创建触发器样例
# init DROP TABLE IF EXISTS students; DROP TABLE IF EXISTS class; # 创建测试用的班级表 CREATE TABLE class ( c ...
- Jquery异步请求数据实例代码
一.Jquery向aspx页面请求数据 前台页面JS代码: 代码如下: $("#Button1").bind("click", function () { $. ...
- Paper Mark2
论文:CBAM: Convolutional Block Attention Module 论文链接 pytorch代码 论文:Approach for Fashion Style Recogniti ...
- c/c++编码规范(3)--google代码规范检测工具cpplint.py
cpplint.py是来自google开源项目风格错误检测工具.它是一个python脚本,和google开源项目风格指南一同发布.下载地址:https://github.com/google/styl ...
- 【c# 学习笔记】封装
封装 指的是把类内部的数据隐藏起来,不让对象实例直接对其操作.c#中提供了属性机制来对类内部的状态进行操作. 在c#中,封装可以通过Public.Private.Protected和Internal等 ...
- Halcon 学习2 金属雕刻字识别
*HALCON 里面的例程名称:engraved_cnn.hdevread_image (Image, 'engraved')get_image_size (Image, Width, Height) ...
- input标签自动填充问题
<input type='text' placeholder='手机号' /> <input type='text' placeholder='地址' /> <input ...
- 1、4 前后端分离,写静态HTML文件,通过ajax 返回数据
1.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti ...
- springboot集成elk 四:springboot + Elasticsearch+Jest
依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spri ...