Leetcode#189. Rotate Arra
Problem
Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.
Example 1:
| 1 | Input: nums = [1,2,3,4,5,6,7], k = 3 | 
Example 2:
| 1 | Input: nums = [-1,-100,3,99], k = 2 | 
Constraints:
- 1 <= nums.length <= 105
- 231 <= nums[i] <= 231 - 1
- 0 <= k <= 105
Follow up:
- Try to come up with as many solutions as you can. There are at least three different ways to solve this problem.
- Could you do it in-place with O(1)extra space?
Solve
錯1
| 1 | class Solution: | 
由於leetcode 判定nums = nums[ n - k:n] + nums[0:n-k]
為建造新的一個變數,所以原先的並未改變
改成(但資測一樣錯的),只是記錄一下leetcode的機制
nums=
→
nums[:]
| 1 | class Solution: | 
解
| 1 | class Solution: | 
%做餘數
確保k 在[0,n-1]
本部落格所有文章除特別聲明外,均採用 CC BY-NC-SA 4.0 許可協議。轉載請註明來自 Imisky!
 評論

