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 ...
随机推荐
- ES的入门学习
ES的入门:ES的雇员文档的设计和实现功能 ES的存放中包括:索引,类型,文档,字段 PUT /megacorp/employee/1{{ "first_name" : " ...
- md5关于加密
# 导入hashlib模块 import hashlib # 获取MD5对象 # 不加盐操作 # md5 = hashlib.md5() # 加盐操作 md5 = hashlib.md5('wenwe ...
- 【概率论】5-9:多项式分布(The Multinomial Distributions)
title: [概率论]5-9:多项式分布(The Multinomial Distributions) categories: - Mathematic - Probability keywords ...
- AtCoder Grand Contest 021题解
传送门 \(A\) 咕咕 ll n,res;bool fl; int main(){ scanf("%lld",&n),fl=1; while(n>9)res+=9, ...
- Django重定向你是如何实现的?用的什么状态码?
使用HttpResponseRedirect redirect和reverse 状态码:302,301
- 模板 - 数学 - 数论 - Min_25筛
终于知道发明者的正确的名字了,是Min_25,这个筛法速度为亚线性的\(O(\frac{n^{\frac{3}{4}}}{\log x})\),用于求解具有下面性质的积性函数的前缀和: 在 \(p\) ...
- [Shell]Docker remote api未授权访问漏洞(Port=2375)
0x01 简介 该未授权访问漏洞是因为docker remote api可以执行docker命令,从官方文档可以看出,该接口是目的是取代docker 命令界面,通过url操作docker. Docke ...
- 【CSP模拟赛】方程(数学)
题目描述 求关于x的方程:x1+x2+……xk=n的非负整数解的个数. 输入格式 仅一行,包含两个正整数n,k. 输出格式 一个整数,表示方程不同解的个数,这个数可能很大,你只需输出mod 20080 ...
- PC 端常用软件
WPS 金山文档 有道云笔记 格式工厂 运行精灵 UC 遨游 360 Firefox 浏览器 光影魔术手 美图秀秀 2345好压 火绒安全软件 有道云笔记 悟空游戏厅 微信 QQ 迅雷 百度网盘 ...
- 11.linux dns服务器建立和安装apache
dns服务器建立 1.安装bind建立dns服务器 yum install bind -y 2.安装好修改配置文件:vim /etc/named.conf 修改: listen-on ...