[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 }
相关文章
相关标签/搜索