Skip to content

Commit d4e1cb6

Browse files
committed
first easy after a happy holiday
1 parent 4f3a4ba commit d4e1cb6

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

move-zeroes/README.md

Whitespace-only changes.

move-zeroes/Solution.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
public class Solution {
2+
3+
void shiftLeft(int[] nums, int p, final int c){
4+
if(c <= 0) return;
5+
6+
while(p < nums.length){
7+
nums[p - c] = nums[p];
8+
9+
if(nums[p] == 0) break;
10+
11+
p++;
12+
}
13+
14+
for(int i = p - c; i < p; i++){
15+
nums[i] = 0;
16+
}
17+
}
18+
19+
public void moveZeroes(int[] nums) {
20+
21+
int i = nums.length - 1;
22+
23+
// trim tailing zero
24+
while(i >= 0 && nums[i] == 0) i--;
25+
26+
int s = i;
27+
28+
boolean metzero = false;
29+
30+
while(i >= 0){
31+
32+
if(nums[i] != 0){
33+
34+
if(metzero){
35+
shiftLeft(nums, s, s - i - 1);
36+
metzero = false;
37+
}
38+
39+
s = i;
40+
} else {
41+
metzero = true;
42+
}
43+
44+
i--;
45+
}
46+
47+
shiftLeft(nums, s, s);
48+
}
49+
}

move-zeroes/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
layout: solution
3+
title: Move Zeroes
4+
date: 2015-09-24 15:55:37+08:00
5+
leetcode_id: 283
6+
---
7+
{% include_relative README.md %}

0 commit comments

Comments
 (0)