-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
31 lines (26 loc) · 762 Bytes
/
Solution.cs
File metadata and controls
31 lines (26 loc) · 762 Bytes
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
namespace LeetCode.Problem905{
//905. Sort Array By Parity
//https://leetcode.com/problems/sort-array-by-parity/
/*
Given an integer array nums, move all the even integers at the beginning of the array followed by all the odd integers.
Return any array that satisfies this condition.
*/
public class Alternative {
public int[] SortArrayByParity(int[] nums) {
int i = 0, j = nums.Length - 1;
while (i < j)
{
while (i < j && nums[i] % 2 == 0)
i++;
while (i < j && nums[j] % 2 !=0)
j--;
if (i < j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
return nums;
}
}
}