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 ...
随机推荐
- mysql 端口号被占用
开始-运行-cmd, 输入 netstat -ano, 看第一列,后面的就是端口,找到3306 ,记住对应的PID!! 然后打开任务管理器 查看 -> 选择列 -> 勾上 PID(进 ...
- Python之sort()函数详解
#从小到大排列 print(sorted([36, 5, -12, 9, -21])) #将待排序的值放入到key中的函数中,在进行比较排序 print(sorted([36, 5, -12, 9, ...
- Hbase表结构
1.Hbase表结构:可以看成map,里面有行键,行键是按照字母顺序排序.行键下面是列族,每个列族可以有不同数量的列甚至是没有列.每个列里面包含着不同时间版本的列的值. 行键:是按照字母的顺序排序的, ...
- Kafka+kylin——kylin2.5.0流式构建
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/a_drjiaoda/article/d ...
- IP地址与Mac地址绑定错误
有个application,有时候可以正常访问,有时候又返回404错误,百思不得其解.刚开始以为是文件夹权限问题,折腾了好久. 后来没在服务器上monitor到包,所以猜想是到了错误的mac地址,用a ...
- 再做一遍floyed
#include<bits/stdc++.h> #define R register int using namespace std; const int inf=0x3f3f3f3f; ...
- 小程序在选择某一个东西的时候,可以用if,else 来做
<view class='fake-select-item-text brand-selected' wx:if='{{selectedBrandName}}'> {{selectedBr ...
- Linux帮助指令
1.介绍 当我们对某个指令不熟悉时,我们可以使用Linux提供的帮助指令来了解这个指令的使用方法. 2.man 指令获得帮助信息 基本语法 man [命令或者配置文件] (功能描述:获得帮助信 ...
- SQL学习笔记(一)
逻辑删除 所谓的逻辑删除其实并不是真正的删除,而是在表中将对应的是否删除标识或者字段做修改操作.在逻辑上数据是被删除的,但数据本身依然存在库中 例如 update students3 set isde ...
- Java 从入门到进阶之路(十六)
在之前的文章我们介绍了一下 Java 中类的多态,本章我们来看一下 Java 中类的内部类. 在 Java 中,内部类分为成员内部类和匿名内部类. 我们先来看一下成员内部类: 1.类中套类,外面的叫外 ...