Skip to content

Commit ab772a1

Browse files
committed
add solution for move zeros task
1 parent a83ccce commit ab772a1

File tree

1 file changed

+47
-0
lines changed
  • C#/LeetCodeResolves/LeetCodeResolves/Arrays/MoveZeroes

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace LeetCodeResolves.Arrays.MoveZeroes
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
var arr = new int[] { 0, 3, 2, 1 };
12+
MoveZeroes(arr);
13+
14+
Console.ReadLine();
15+
}
16+
17+
static void MoveZeroes(int[] arr)
18+
{
19+
var slow = 0;
20+
var fast = 0;
21+
while (fast < arr.Length)
22+
{
23+
if (arr[slow] == 0)
24+
{
25+
while (fast < arr.Length && arr[fast] == 0)
26+
{
27+
fast++;
28+
}
29+
30+
if (fast == arr.Length)
31+
{
32+
return;
33+
}
34+
else
35+
{
36+
var temp = arr[slow];
37+
arr[slow] = arr[fast];
38+
arr[fast] = temp;
39+
}
40+
}
41+
42+
slow++;
43+
fast++;
44+
}
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)