[LeetCode]12. Rotate Array旋转数组

Rotate an array of n elements to the right by k steps.

For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].

Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.

[show hint]

Hint:
Could you do it in-place with O(1) extra space?

Related problem: Reverse Words in a String II

解法1:一个最简单的想法就是每次将数组后移一步,一共循环k次。满足in-place,但是时间复杂度为O(k*n)。

class Solution {
public:
    void rotate(vector<int>& nums, int k) {
        int n = nums.size();
        if(n < 2 || k < 1)
            return;

        for(int i = 1; i <= k; i++)
        {
            int tmp = nums[n - 1];
            for(int j = n - 1; j > 0; j--)
                nums[j] = nums[j - 1];
            nums[0] = tmp;
        }
    }
};

解法2:[1,2,3,4,5,6,7,8],k=3,旋转后为[6,7,8,1,2,3,4,5],对比前后每个元素的下标发现规律index2=(index1+k)%n,n为数组长度。时间复杂度为O(n),空间复杂度O(n)。

class Solution {
public:
    void rotate(vector<int>& nums, int k) {
        int n = nums.size();
        if(n < 2 || k < 1)
            return;

        vector<int> res(n, 0);
        for(int i = 0; i < n; i++)
            res[(i + k) % n] = nums[i];
        copy(res.begin(), res.end(), nums.begin());
    }
};

解法3:[1,2,3,4,5,6,7,8],k=3,旋转后为[6,7,8,1,2,3,4,5],可以分2步进行:(1)将整个数组旋转(k=n),得到[8,7,6,5,4,3,2,1];(2)以k为分界点,分别旋转第一次旋转后数组的前后两部分,即[8,7,6]和[5,4,3,2,1],得到[6,7,8]和[1,2,3,4,5],即是需要的结果。时间复杂度O(n),空间复杂度O(1)。

注意:需要将k归化到小于n的情形下。因为k=n时相当于不旋转,因此可以重新取k=k%n。

class Solution {
public:
    void rotate(vector<int>& nums, int k) {
        int n = nums.size();
        k %= n;
        reverse(nums.begin(), nums.end());
        reverse(nums.begin(), nums.begin() + k);
        reverse(nums.begin() + k, nums.end());
    }
};

或者以n - k为界,分别对数组的左右两边执行一次逆置;然后对整个数组执行逆置。

class Solution {
public:
    void rotate(vector<int>& nums, int k) {
        int n = nums.size();
        k %= n;
        reverse(nums.begin(), nums.begin() + n - k);
        reverse(nums.begin() + n - k, nums.end());
        reverse(nums.begin(), nums.end());
    }
};
时间: 2024-10-06 13:40:16

[LeetCode]12. Rotate Array旋转数组的相关文章

[LeetCode] Rotate Array 旋转数组

Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to solve this pro

rotate array 旋转数组

class Solution {public: void rotate(vector<int>& nums, int k) { int n=nums.size(); int i=0; /* //------------------- //解法一  会超时 //-------------------- k=k%n; while(i<k){ int temp=nums[n-1]; for(int j=n-1;j>0;j--){ nums[j]=nums[j-1]; } nums

[LeetCode] 61. Rotate List 旋转链表

Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: Input: 1->2->3->4->5->NULL, k = 2 Output: 4->5->1->2->3->NULL Explanation: rotate 1 steps to the right: 5->1->2->3-&g

LeetCode:Rotate Array

1.题目名称 Rotate Array(平移数组) 2.题目地址 https://leetcode.com/problems/rotate-array/ 3.题目内容 英文:Rotate an array of n elements to the right by k steps. 中文:将一个长度为n的数组,向右平移k个单位 4.解题方法1 一个比较易于理解的方法是新开辟一个与原数组等长的数组,循环考察原数组的元素,将它们放到新数组中平移后的位置上,最后再将新数组上的元素赋回原数组. 一段可以

LeetCode 189. Rotate Array (旋转数组)

Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to solve this pro

LeetCode Rotate Array 翻转数组

题意:给定一个数组,将该数组的后k位移动到前n-k位之前.(本题在编程珠玑中第二章有讲) 思路: 方法一:将后K位用vector容器装起来,再移动前n-k位到后面,再将容器内k位插到前面. 1 class Solution { 2 public: 3 void rotate(int nums[], int n, int k) { 4 if( !k || !n || n==1 || k==n ) return; 5 k %= n; 6 vector<int> cha; 7 cha.reserve

(每日算法)LeetCode --- Search in Rotated Sorted Array(旋转数组的二分检索)

Search in Rotated Sorted Array I && II Leetcode 对有序数组进行二分查找(下面仅以非递减数组为例): int binarySort(int A[], int lo, int hi, int target) { while(lo <= hi) { int mid = lo + (hi - lo)/2; if(A[mid] == target) return mid; if(A[mid] < target) lo = mid + 1;

leetCode 189. Rotate Array 数组

189. Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways

[LeetCode]25. Search in Rotated Array旋转数组查找

Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplic