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 ...
随机推荐
- nachos3.4 threads管理 (c++)
Main.cc //引导代码初始化操作系统内核.允许直接调用内部操作系统功能,简化调试和测试.在实践中,bootstrap代码只会初始化数据结构, //并启动一个用户程序来打印登录提示.//许多内容只 ...
- 模拟赛 提米树 题解 (DP+思维)
题意: 有一棵棵提米树,满足这样的性质: 每个点上长了一定数量的Temmie 薄片,薄片数量记为这个点的权值,这些点被标记为 1 到 n 的整数,其 中 1 号点是树的根,没有孩子的点是树上的叶子. ...
- spark yarn 提交作业
spark提交作业命令: ./spark-submit --master yarn --deploy-mode cluster --class com.zjlantone.hive.SparkOper ...
- Hive 模式设计
Hive看上去很像关系型数据库.不过,Hive实现和使用的方式和传统的关系型数据库非常不同.Hive是反模式的. 本文将重点介绍Hive中哪些模式是用户应该使用的,儿哪些是应该避免的 一.按天划分的表 ...
- my.conf
[client] port = 3306 socket = /tmp/mysql.sock [mysqld] port = 3306 socket = /tmp/mysql.sock basedir ...
- AtCoder Grand Contest 038 题解
传送门 这场表现的宛如一个\(zz\) \(A\) 先直接把前\(b\)行全写成\(1\),再把前\(a\)列取反就行 const int N=1005; char mp[N][N];int n,m, ...
- Mac laravel: command not found
如果用的oh-my-zsh 安装laravel 提示找不到.可以试试下面的 export PATH=$HOME/bin:/usr/local/bin:~/.composer/vendor/bin:$P ...
- 【转】浅析Linux中的零拷贝技术
本文探讨Linux中主要的几种零拷贝技术以及零拷贝技术适用的场景.为了迅速建立起零拷贝的概念,我们拿一个常用的场景进行引入: 引文## 在写一个服务端程序时(Web Server或者文件服务器),文件 ...
- day25 内置常用模块(四): 模块和包
阅读目录: 模块 import from xxx import xxx 包 import from xxx import xxx from xxx import * __init__.p ...
- 各种DTO类最好有 无参数的构造方法
以一下这个类为例 @Getter @Setter @ToString class Person { private String s; public Person(String s) { this.s ...