LeetCode:Rotate Array

来源:互联网 发布:终极算法 改变世界 编辑:程序博客网 时间:2024/05/17 05:50

想了半天最后还是用了暴力解法,总觉得应该还有更巧妙的算法。

class Solution {public:    void rotate(int nums[], int n, int k) {        int temp;        int i,j;        int count;        k=n-k%n;//将向右旋转变成向左旋转,但注意k的值有可能比n大        if(k==n)            return;        else{        count=0;        while(count<k)        {            temp=nums[0];            for(i=1;i<n;i++)            {                nums[i-1]=nums[i];            }            nums[n-1]=temp;            count++;        }                    }    }};



0 0
原创粉丝点击