-
Notifications
You must be signed in to change notification settings - Fork 5
/
move_zeroes.dart
80 lines (61 loc) · 1.46 KB
/
move_zeroes.dart
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
/*
-* Move Zeroes *-
Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.
Note that you must do this in-place without making a copy of the array.
Example 1:
Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]
Example 2:
Input: nums = [0]
Output: [0]
Constraints:
1 <= nums.length <= 104
-231 <= nums[i] <= 231 - 1
Follow up: Could you minimize the total number of operations done?
*/
class Solution {
void moveZeroes(List<int> nums) {
int zero = 0;
for (int index = 0; index < nums.length; index++) {
if (nums[index] == 0) {
zero++;
} else if (zero > 0) {
int tempBall = nums[index];
nums[index] = 0;
nums[index - zero] = tempBall;
}
}
}
}
class B {
void moveZeroes(List<int> nums) {
int index = 0;
for (int i = 0; i < nums.length; i++) {
nums[index++] = nums[i];
}
while (index < nums.length) {
nums[index] = 0;
index++;
}
}
}
class C {
void moveZeroes(List<int> nums) {
int left = 0;
int right = 1;
while (left < nums.length && right < nums.length) {
if (nums[left] == 0 && nums[right] != 0) {
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
} else if (nums[left] == 0 && nums[right] == 0) {
right++;
} else if (nums[left] != 0) {
left++;
right++;
} else {
left++;
}
}
}
}