Skip to content

Commit 1c20bf6

Browse files
wiggle sort
1 parent bec0cbc commit 1c20bf6

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

MEDIUM/src/medium/WiggleSort.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package medium;
2+
3+
/** Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3]....
4+
For example, given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4]*/
5+
public class WiggleSort {
6+
public void wiggleSort(int[] nums) {
7+
for(int i = 1; i < nums.length; i++){
8+
if((i%2 == 0 && nums[i] > nums[i-1]) || (i%2 == 1 && nums[i] < nums[i-1])) swap(nums, i);
9+
}
10+
}
11+
12+
void swap(int[] nums, int i){
13+
int temp = nums[i-1];
14+
nums[i-1] = nums[i];
15+
nums[i] = temp;
16+
}
17+
}

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
|325|[Maximum Size Subarray Sum Equals k](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/)|[Solution] | O(n)|O(n) | Medium| HashMap
1818
|314|[Binary Tree Vertical Order Traversal](https://leetcode.com/problems/binary-tree-vertical-order-traversal/)|[Solution](../../blob/master/MEDIUM/src/medium/BinaryTreeVerticalOrderTraversal.java)| O(n)|O(n) | Medium| HashMap, BFS
1919
|301|[Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/)|[Solution]| ? | ? | Hard| BFS
20-
|283|[Move Zeroes](https://leetcode.com/problems/move-zeroes/)|[Solution](../../blob/master/EASY/src/easy/MoveZeroes.java)| O(n)|O(1) | Easy|
2120
|292|[Nim Game](https://leetcode.com/problems/nim-game/)|[Solution](../../blob/master/EASY/src/easy/NimGame.java)| O(1)|O(1) | Easy|
21+
|283|[Move Zeroes](https://leetcode.com/problems/move-zeroes/)|[Solution](../../blob/master/EASY/src/easy/MoveZeroes.java)| O(n)|O(1) | Easy|
22+
|280|[Wiggle Sort](https://leetcode.com/problems/wiggle-sort/)|[Solution](../../blob/master/MEDIUM/src/medium/WiggleSort.java)| O(n)|O(n) | Medium|
2223
|278|[First Bad Version](https://leetcode.com/problems/first-bad-version/)|[Solution](../../blob/master/EASY/src/easy/FirstBadVersion.java)| O(logn)|O(1) | Easy| Binary Search
2324
|273|[Integer to English Words](https://leetcode.com/problems/integer-to-english-words/)|[Solution]|
2425
|257|[Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/)|[Solution](../../blob/master/EASY/src/easy/BinaryTreePaths.java) | O(n*h) | O(h) | DFS/Recursion

0 commit comments

Comments
 (0)