[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 ...
随机推荐
- java实现https免证书认证
java实现https免证书认证 解决方法: 1.下载两个包,httpclient-4.2.jar和httpcore-4.2.jar,复制以下代码就可使用. 2.调用类代码: String htt ...
- MauiMETA工具的使用(一)
MauiMETA工具的使用(一) 摘自:https://www.jianshu.com/p/a377119947f8 tianxiaoMCU 关注 2018.12.21 14:15 字数 267 ...
- 123457---脑筋急转弯01--com.threeObj03.JiZhuanWan
脑筋急转弯01--com.threeObj03.JiZhuanWan
- Intellij-编码设置
目录 文件编码修改 @(目录) 文件编码修改 • 上图标注 1 所示,IDE 的编码默认是 UTF-8 , Project Encoding 虽然默认是 GBK ,但是一般都建议 修改为 UTF-8 ...
- laravel5.2总结
转自:http://www.cnblogs.com/redirect/p/6178001.html
- 【Leetcode_easy】766. Toeplitz Matrix
problem 766. Toeplitz Matrix solution1: class Solution { public: bool isToeplitzMatrix(vector<vec ...
- 云计算服务模式(SaaS/PaaS/IaaS)
为什么是云计算,为什么是现在 商用云:商用云的设计初衷是将基础设施商品化,并以较低的成本对外提供,是用户能够获得高扩展性和自服务能力. 企业云:企业级云的目的,则是达到或超过它所要替代的本地基础设施的 ...
- C语言 班级档案管理系统实现
代码地址:github地址 班级档案管理系统 原题目要求是对一个有N个学生的班级,通过该系统实现对该班级学生的基本信息进行录入. 显示.修改.删除.保存等操作的管理. 由于个人需要,我单独将项目改造为 ...
- Mybatis使用Spring data Pageable的方法
引言 可能这个用法是个邪教了...但是简单说这都是历史缘故,貌似是项目最初用JPA后面还是换Mybatis了,我接手时候看着那个写好的Controller层觉得换了怪可惜的,就沿用了.网上找找,提供的 ...
- QT信号槽连接
一:信号槽是什么? Qt的信号和槽机制是Qt的一大特点,实际上这是和MFC中的消息映射机制相似的东西,要完成的事情也差不多,就是发送一个消息然后让其它窗口响应,当然,这里的消息是广义的说法,简单点说就 ...