[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 ...
随机推荐
- Day5作业,商城+ATM机+后台管理
晚来了....东西太多,需要写的blog内容太多,re讲的渣渣,不明白为什么oldboy经常换老师,吐槽下吧,真心不爱了.... github地址在这:https://github.com/ccorz ...
- LeetCode_28. Implement strStr()
28. Implement strStr() Easy Implement strStr(). Return the index of the first occurrence of needle i ...
- Release报错Debug无错
代码在Release模式下会crash,Debug模式下可以运行,最后定位到原因 for (size_t j = 0; j < ids.size()-1; ++j) { } 发现问题是Relea ...
- html设置多个div并排显示
我这里以4个div为例,html代码如下: <body> <div id="column1" style="background-color: blue ...
- jQuery BlockUI Plugin Demo 4(Element Blocking Examples)
Element Blocking Examples This page demonstrates how to block selected elements on the page rather t ...
- iOS-UITabbar自定义
[self createCustomTabBar]; -(void)createCustomTabBar{ //创建一个UIImageView,作为底图 UIImageView *bgVi ...
- Spring mvc +ajax 发送邮件
1.前端页面--form表单提交,通过发送按钮的id=“send”定位DOM,触发ajax请求 <form class="form-horizontal" id=" ...
- Laravel增加CORS中间件完成跨域请求
原文地址: 跨域的请求 出于安全性的原因,浏览器会限制 Script 中的跨域请求.由于 XMLHttpRequest 遵循同源策略,所有使用 XMLHttpRequest 构造 HTTP 请求的应用 ...
- SPSS 2019年10月31日 20:20:53今日学习总结
◆描述性统计分析 概念:描述性统计分析方法是指应用分类.制表.图形及概括性数据指标(去均值,方差等)来概括数据分布特征的方法. 而推断性统计分析方法则是通过随机抽样,应用统计方法把从样本数据得到的结论 ...
- go os.State类用法
参考文章: https://blog.csdn.net/weixin_43851310/article/details/87988648