-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path283.move-zeroes.java
93 lines (86 loc) · 2.08 KB
/
283.move-zeroes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* @lc app=leetcode id=283 lang=java
*
* [283] Move Zeroes
*
* https://leetcode.com/problems/move-zeroes/description/
*
* algorithms
* Easy (58.47%)
* Total Accepted: 1M
* Total Submissions: 1.8M
* Testcase Example: '[0,1,0,3,12]'
*
* 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.
*
*/
class Solution {
public void moveZeroes(int[] nums) {
if (nums == null || nums.length == 0) return;
// Find all non zero elements and shift them are the beginning of the array
// in order, disregarding if will be duplicated
//
// Loop over array from the index adjacent to the last non-zero element from
// the first loop, and fill those remaining elements with zeroes.
//
// Input: [0,1,0,3,12]
// [1,3,0,3,12]
// [1,3,12,0,12]
// [1,3,12,0,0]
// [1,3,12,0,0]
int anchor = 0;
for (int num : nums) {
if (num != 0) nums[anchor++] = num;
}
while (anchor < nums.length) {
nums[anchor++] = 0;
}
}
// First working solution (SLOWER)
// public void moveZeroes(int[] nums) {
// // Notes:
// // Zeroes are scattered everywhere
// // Modify array in place
// //
// // Input: [0,1,0,3,12]
// // [1,0,0,3,12]
// // [1,3,0,0,12]
// // [1,3,12,0,0]
//
// // Loop over the array
// // Check if current value is 0
// // Loop over the array from current position until a non-zero element
// // has been found
// // Swap the current value from outer loop with the non-zero element
// // found in the inner loop
//
// for (int i = 0; i < nums.length - 1; i++) {
// int cur = nums[i];
//
// if (cur == 0) {
//
// for (int j = i + 1; j < nums.length; j++) {
// if (nums[j] != 0) {
// nums[i] = nums[j];
// nums[j] = cur;
// break;
// }
// }
// }
// }
//
// }
}