Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1c20bf6

Browse files
wiggle sort
1 parentbec0cbc commit1c20bf6

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

‎MEDIUM/src/medium/WiggleSort.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
packagemedium;
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+
publicclassWiggleSort {
6+
publicvoidwiggleSort(int[]nums) {
7+
for(inti =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+
voidswap(int[]nums,inti){
13+
inttemp =nums[i-1];
14+
nums[i-1] =nums[i];
15+
nums[i] =temp;
16+
}
17+
}

‎README.md

Lines changed: 2 additions & 1 deletion
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)

[8]ページ先頭

©2009-2025 Movatter.jp