Skip to content

Commit

Permalink
add edge case to Rotate Array
Browse files Browse the repository at this point in the history
  • Loading branch information
wanghaiqian committed Jul 2, 2019
1 parent fb2b843 commit 703511f
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions Array/RotateArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@

class RotateArray {
func rotate(_ nums: inout [Int], _ k: Int) {
var k = k % nums.count

guard nums.count > 0 && k > 0 else {
return
}
let k = k % nums.count
guard k != 0 else {
return
}
_reverse(&nums, 0, nums.count - 1)
_reverse(&nums, 0, k - 1)
_reverse(&nums, k, nums.count - 1)
Expand All @@ -36,4 +41,4 @@ class RotateArray {
private func _swap<T>(_ nums: inout Array<T>, _ p: Int, _ q: Int) {
(nums[p], nums[q]) = (nums[q], nums[p])
}
}
}

0 comments on commit 703511f

Please sign in to comment.