mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-14 01:49:41 +01:00
cs: add “283. Move Zeroes”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
0178e14f48
commit
64008089ff
1 changed files with 31 additions and 0 deletions
31
cs/move-zeroes.cs
Normal file
31
cs/move-zeroes.cs
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
public class Solution {
|
||||||
|
public void MoveZeroes(int[] nums) {
|
||||||
|
var i = 0;
|
||||||
|
// find first zero
|
||||||
|
while (i < nums.Length && nums[i] != 0) {
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var j = i + 1; j < nums.Length; ++j) {
|
||||||
|
// we do nothing for zeroes
|
||||||
|
if (nums[j] == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// find first zero
|
||||||
|
while (i < nums.Length && nums[i] != 0) {
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
|
||||||
|
// no zero has been found
|
||||||
|
if (i >= nums.Length) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// swap them while maintaining the order
|
||||||
|
nums[i] = nums[j];
|
||||||
|
nums[j] = 0;
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue