-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0283-move-zeroes.cs
40 lines (38 loc) · 919 Bytes
/
0283-move-zeroes.cs
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
public class Solution
{
public void MoveZeroes(int[] nums)
{
int readIndex = 0;
int writeIndex = 0;
while (readIndex < nums.Length)
{
if (nums[readIndex] == 0)
{
readIndex++;
continue;
}
if (readIndex != writeIndex)
{
nums[writeIndex] = nums[readIndex];
nums[readIndex] = 0;
}
writeIndex++;
readIndex++;
}
}
}
public class NeetCodeWaySolution { //NeetCodeWay
public void MoveZeroes(int[] nums) {
if (nums.Length <= 1) return;
int l = 0, r = 0;
while (r < nums.Length) {
if (nums[r] != 0) {
var t = nums[l];
nums[l++] = nums[r];
nums[r++] = t;
} else {
++r;
}
}
}
}