-
Notifications
You must be signed in to change notification settings - Fork 23
/
Partition Array by Odd and Even.java
executable file
·60 lines (52 loc) · 1.47 KB
/
Partition Array by Odd and Even.java
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
E
tags: Two Pointers, Array
- 更正常的start/end partition pointer类似: when condition meet, swap
- Clean up TODO
```
/*
Partition an integers array into odd number first and even number second.
Example
Given [1, 2, 3, 4], return [1, 3, 2, 4]
Challenge
Do it in-place.
Tags Expand
Two Pointers Array
Thougths:
Use two pointers: nextOddPt, firstEvenPt
1. Whenever nextOddPt > firstEvenPt, swapt them
2. Incrase nextOddPt in a for loop
Note:
After each swap, have to start checking again from beginning-switching point, which will be firstEvenPt. Need to set i = firstEvenPt.
However, since for loop will do i++, we need to set i = firstEvenPt - 1;
And firstEvenPt only needs to be update once so use -1 to check if it's set.
*/
public class Solution {
/**
* @param nums: an array of integers
* @return: nothing
*/
public void partitionArray(int[] nums) {
if (nums == null || nums.length == 0){
return;
}
int nextOddPt = -1;
int firstEvenPt = -1;
for (int i = 0; i < nums.length; i++) {
if (nums[i] % 2 == 1) {
nextOddPt = i;
} else {
if (firstEvenPt == -1) {
firstEvenPt = i;
}
}
if (nextOddPt > firstEvenPt && firstEvenPt != -1) {
int temp = nums[nextOddPt];
nums[nextOddPt] = nums[firstEvenPt];
nums[firstEvenPt] = temp;
i = firstEvenPt - 1;
firstEvenPt = -1;
}
}
}
}
```