HappyLeetcode50: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].

这道题虽然做了出来,但是显然不是最优解。显然空间要求没有达到。

有一点要尤为注意,在一开始的时候我就默认k是小于n的,实际上k可以大于n,所以在修改代码之后,我们所使用的k要对n取余。

我的思路是,把数组中前面的值先挪到后面去。后面的值在被替换之前先存进一个vector里面去,待前面的值安排完毕之后,把vector之中的数据取出,放在新数组的前面。

代码如下:

class Solution {
public:
    void rotate(int nums[], int n, int k) {

        if (nums == NULL || n <= 0 )
            return;

        k = k%n;

        vector<int> TempSave;

        TempSave.reserve(k);
        int count = 0;//记录已经推进了多少个数据
        for (int i = n - k - 1; i >= 0; --i)
        {
            if (count < k){
                TempSave.push_back(nums[i + k]);
                count++;
            }

            nums[i + k] = nums[i];
        }

        int Temp = TempSave.size();
        if (TempSave.size() < k)
        {
            for (int i = k - 1; i >= Temp; --i)
                TempSave.push_back(nums[i]);
        }

        for (int i = 0; i < k; ++i)
        {
            nums[i] = TempSave.back();
            TempSave.pop_back();
        }
    }
};

其实后来想想还是有更简单一些的方法,比如先把数组复制一遍,再根据情况裁剪出你需要的那一部分数组出来。

class Solution {
public:
    void rotate(int nums[], int n, int k) {
        k = k % n;
        int* NewNums =new int[n * 2];
        for (int i = 0; i < n ; ++i)
        {
            NewNums[i] = nums[i];
            NewNums[i + n] = nums[i];
        }
        //int res = n - k;
        for (int i = 0; i < n; ++i)
        {
            nums[i] = NewNums[i + n - k];
        }
    }
};

代码量明显短了不少。

时间: 2024-08-27 05:52:39

HappyLeetcode50:Rotate Array的相关文章

[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

189. Rotate Array

1. 问题描述 189. Rotate ArrayRotate 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

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】189. Rotate Array

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 s

151. Reverse Words in a String &amp;&amp; 61. Rotate List &amp;&amp; 189. Rotate Array

151. Reverse Words in a String Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". What constitutes a word?A sequence of non-space characters constitutes a word. Cou

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

189. Rotate Array【easy】

189. Rotate Array[easy] 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 differen

2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe

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]. 思路: 我的思路:新建一个数组存放旋转后的内容,但是怎么把原数组的内容存放在数组中,不清楚. leetcode/discuss思路: 思路一:新建数组,复制原

LeetCode:(Array-189) Rotate Array

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 s