LeetCode_283. Move Zeroes
283. Move Zeroes
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
Example:
Input:[0,1,0,3,12]
Output:[1,3,12,0,0]
Note:
- You must do this in-place without making a copy of the array.
- Minimize the total number of operations.
package leetcode.easy;
public class MoveZeroes {
public void moveZeroes1(int[] nums) {
int n = nums.length;
// Count the zeroes
int numZeroes = 0;
for (int i = 0; i < n; i++) {
if (nums[i] == 0) {
numZeroes++;
}
}
// Make all the non-zero elements retain their original order.
java.util.ArrayList<Integer> ans = new java.util.ArrayList<Integer>();
for (int i = 0; i < n; i++) {
if (nums[i] != 0) {
ans.add(nums[i]);
}
}
// Move all zeroes to the end
while (numZeroes > 0) {
ans.add(0);
numZeroes--;
}
// Combine the result
for (int i = 0; i < n; i++) {
nums[i] = ans.get(i);
}
}
public void moveZeroes2(int[] nums) {
int lastNonZeroFoundAt = 0;
// If the current element is not 0, then we need to
// append it just in front of last non 0 element we found.
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 0) {
nums[lastNonZeroFoundAt++] = nums[i];
}
}
// After we have finished processing new elements,
// all the non-zero elements are already at beginning of array.
// We just need to fill remaining array with 0's.
for (int i = lastNonZeroFoundAt; i < nums.length; i++) {
nums[i] = 0;
}
}
public void moveZeroes3(int[] nums) {
for (int lastNonZeroFoundAt = 0, cur = 0; cur < nums.length; cur++) {
if (nums[cur] != 0) {
int temp = nums[lastNonZeroFoundAt];
nums[lastNonZeroFoundAt] = nums[cur];
nums[cur] = temp;
lastNonZeroFoundAt++;
}
}
}
private void print_arr(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}
@org.junit.Test
public void test1() {
int[] nums = { 0, 1, 0, 3, 12 };
print_arr(nums);
moveZeroes1(nums);
print_arr(nums);
}
@org.junit.Test
public void test2() {
int[] nums = { 0, 1, 0, 3, 12 };
print_arr(nums);
moveZeroes2(nums);
print_arr(nums);
}
@org.junit.Test
public void test3() {
int[] nums = { 0, 1, 0, 3, 12 };
print_arr(nums);
moveZeroes3(nums);
print_arr(nums);
}
}
LeetCode_283. Move Zeroes的更多相关文章
- LeetCode:Move Zeroes
LeetCode:Move Zeroes [问题再现] Given an array nums, write a function to move all 0's to the end of it w ...
- [LintCode] Move Zeroes 移动零
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- 【5_283】Move Zeroes
终于碰到一道水题,睡觉去~ Move Zeroes Total Accepted: 37369 Total Submissions: 88383 Difficulty: Easy Given an a ...
- Leetcode-283 Move Zeroes
#283. Move Zeroes Given an array nums, write a function to move all 0's to the end of it while mai ...
- 283. Move Zeroes(C++)
283. Move Zeroes Given an array nums, write a function to move all 0's to the end of it while mainta ...
- leetcode之旅(7)-Move Zeroes
Move Zeroes 题目描述: Given an array nums, write a function to move all 0's to the end of it while maint ...
- LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List
283. Move Zeroes var moveZeroes = function(nums) { var num1=0,num2=1; while(num1!=num2){ nums.forEac ...
- 【leetcode】283. Move Zeroes
problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...
- 【leetcode】Move Zeroes
Move Zeroes 题目: Given an array nums, write a function to move all 0‘s to the end of it while maintai ...
随机推荐
- 洛谷 P1613 跑路 题解
P1613 跑路 题目描述 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是为了保住自己的工资,小A买了一个十分牛B的 ...
- nginx+uwsgi+python3+pipenv+mysql+redis部署django程序
1.下载项目 git clone https://github.com/wangyitao/MyBlogs.git 2.进入Myblogs目录 cd MyBlogs 3.创建虚拟环境并且安装依赖 pi ...
- flutter 监听返回
在项目中遇到了一个场景,A页面必须返回某个tab页,但是A页面可能会调到B,再跳到C,最后回到A.这个时候A的返回肯定是C. 想了一些解决方案,都不如监听A页面的实体键返回或者虚拟键返回来的快速便捷. ...
- P1041 传染病控制——暴力遍历所有相同深度的节点
P1041 传染病控制 说实话这种暴力我还是头一次见,每次病毒都会往下传染一层: 数据范围小,我们可以直接枚举当前层保护谁就好了: 用vector 记录相同层数的节点:维护已经断了的点: 如果超出最底 ...
- WARNING: You are using pip version 19.1.1, however version 19.2.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command.
pip3 install --upgrade pip
- Nginx-实践篇(重要)
原文链接:https://blog.csdn.net/Fe_cow/article/details/84672361
- Sqlmap全参数详解
sqlmap全参数详解 sqlmap是在sql注入中非常常用的一款工具,由于其开源性,适合从个人到企业,从学习到实战,各领域各阶段的应用,我们还可以将它改造成我们自己独有的渗透利器.这款工具中,大大小 ...
- Vue 使用axios分片上传
Vue的界面 <input type="file"/> 上传方法 fileUpload: function () { var num = 1 var file = do ...
- GIS地理工具案例教程——批量合并影像
GIS地理工具案例教程——批量合并影像 商务合作,科技咨询,版权转让:向日葵,135—4855__4328,xiexiaokui#qq.com 描述:合并目录下的所有影像 功能:对指定工作空间下的栅格 ...
- vue 自己编写向左滑动的动画 仿transition
vue 模板代码: <div class="content-wrap clearfix" :class="{slideIn: showIn, slideOut: s ...