Skip to content

Commit

Permalink
0280-wiggle-sort
Browse files Browse the repository at this point in the history
  • Loading branch information
bofeizhu committed Aug 20, 2018
1 parent 024fc6a commit 40a05bf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
27 changes: 27 additions & 0 deletions 0280-wiggle-sort.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/// 280. Wiggle Sort
/// Given an unsorted array nums, reorder it in-place such that
/// nums[0] <= nums[1] >= nums[2] <= nums[3]....

import XCTest

/// Approach: Swap
func wiggleSort(_ nums: inout [Int]) {
guard nums.count > 1 else { return }
for i in 0..<nums.count - 1 {
if (i % 2 == 0 && nums[i] > nums[i + 1]) ||
(i % 2 != 0 && nums[i] < nums[i + 1]) {
nums.swapAt(i, i + 1)
}
}
}

class Tests: XCTestCase {
func testExample() {
var nums = [3, 5, 2, 1, 6, 4]
let expected = [3, 5, 1, 6, 2, 4]
wiggleSort(&nums)
XCTAssertEqual(nums, expected)
}
}

Tests.defaultTestSuite.run()
4 changes: 4 additions & 0 deletions 0280-wiggle-sort.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
<timeline fileName='timeline.xctimeline'/>
</playground>
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ You can visit the pages below to search problems by company tags. Then come back
163 | [Missing Ranges](https://leetcode.com/problems/missing-ranges/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0163-missing-ranges.playground/Contents.swift) | Iteration
200 | [Number of Islands](https://leetcode.com/problems/number-of-islands/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0200-number-of-islands.playground/Contents.swift) | BFS
259 | [Three Sum Smaller](https://leetcode.com/problems/3sum-smaller/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0259-3sum-smaller.playground/Contents.swift) | Two Pointers
280 | [Wiggle Sort](https://leetcode.com/problems/wiggle-sort/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0280-wiggle-sort.playground/Contents.swift) | Swap
482 | [License Key Formatting](https://leetcode.com/problems/license-key-formatting/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0482-license-key-formatting.playground/Contents.swift) | String
681 | [Next Closest Time](https://leetcode.com/problems/next-closest-time/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0681-next-closest-time.playground/Contents.swift) | Simulation
683 | [K Empty Slots](https://leetcode.com/problems/k-empty-slots/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0683-k-empty-slots.playground/Contents.swift) | Sliding Window
Expand Down

0 comments on commit 40a05bf

Please sign in to comment.