[Swift]LeetCode189. 旋轉數組 | Rotate Array

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-tbwtgygt-md.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given an array, rotate the array to the right by k steps, where k is non-negative.git

Example 1:github

Input:  and k = 3
Output: 
Explanation:
rotate 1 steps to the right: 
rotate 2 steps to the right: rotate 3 steps to the right: 
[1,2,3,4,5,6,7][5,6,7,1,2,3,4][7,1,2,3,4,5,6][6,7,1,2,3,4,5][5,6,7,1,2,3,4]

Example 2:算法

Input:  and k = 2
Output: [3,99,-1,-100]
Explanation: 
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]
[-1,-100,3,99]

Note:數組

  • Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
  • Could you do it in-place with O(1) extra space?

給定一個數組,將數組中的元素向右移動 個位置,其中 是非負數。微信

示例 1:app

輸入:  和 k = 3
輸出: 
解釋:
向右旋轉 1 步: 
向右旋轉 2 步: 向右旋轉 3 步: 
[1,2,3,4,5,6,7][5,6,7,1,2,3,4][7,1,2,3,4,5,6][6,7,1,2,3,4,5][5,6,7,1,2,3,4]

示例 2:this

輸入:  和 k = 2
輸出: [3,99,-1,-100]
解釋: 
向右旋轉 1 步: [99,-1,-100,3]
向右旋轉 2 步: [3,99,-1,-100][-1,-100,3,99]

說明:spa

  • 儘量想出更多的解決方案,至少有三種不一樣的方法能夠解決這個問題。
  • 要求使用空間複雜度爲 O(1) 的原地算法。

48mscode

 1 class Solution {
 2     func rotate(_ nums: inout [Int], _ k: Int) {
 3         guard nums.count > 1 else {
 4             return
 5         }
 6         
 7         guard k > 0 else {
 8             return
 9         }
10         
11         let k = k % nums.count        
12         nums = Array(nums[(nums.count - k)..<nums.count]) + Array(nums[0..<(nums.count - k)])        
13     }
14 }

48ms

 1 class Solution {
 2     func rotate(_ nums: inout [Int], _ k: Int) {
 3          let n = nums.count
 4         let numRoations = k % n
 5         var rotated = [Int]()
 6         for i in n-numRoations..<n {
 7             rotated.append(nums[i])
 8         }
 9         for j in 0..<n-numRoations {
10             rotated.append(nums[j])
11         }
12         nums = rotated
13     }
14 }

52ms

 1 class Solution {
 2     func rotate(_ nums: inout [Int], _ k: Int) {
 3         rotate1(&nums, k)
 4     }
 5     
 6     func rotate1(_ nums: inout [Int], _ k: Int) {
 7         var r: [Int] = Array(repeating: 0, count: nums.count)
 8         var kk = nums.count - (k % nums.count)
 9         for i in 0..<nums.count {
10             r[i] = nums[(i + kk) % nums.count]
11         }
12         for (i, num) in r.enumerated() {
13             nums[i] = num
14         }
15     }
16 }

56ms

 1 class Solution {
 2     func rotate(_ nums: inout [Int], _ k: Int) {
 3         guard nums.count > 0, k > 0 else {
 4             return
 5         }
 6         
 7         let k = k%nums.count
 8         
 9         rotate(&nums, 0, nums.count-k-1)
10         rotate(&nums, nums.count-k, nums.count-1)
11         rotate(&nums, 0, nums.count-1)
12     }
13     
14     private func rotate(_ nums: inout [Int], _ left: Int, _ right: Int) {
15         if left == right || nums.count == 0 { return }
16         
17         var i = left, j = right
18         
19         while i < j {
20             let temp = nums[i]
21             nums[i] = nums[j]
22             nums[j] = temp
23             i += 1
24             j -= 1
25         }
26     }
27 }

64ms

1 class Solution {
2     func rotate(_ nums: inout [Int], _ k: Int) {
3         var n = nums.count
4         let ind = n-1-k%n
5         nums += nums[0...ind]
6         nums.removeSubrange(0...ind)
7     }
8 }
相關文章
相關標籤/搜索