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]

Related problem: Reverse Words in a String II

分析:题意为 将n个元素的数组向右旋转k步

思路:用vector容器的东西来做很简单

代码如下:(O(1) Space)

class Solution {
public:
void rotate(vector<int>& nums, int k)
{
for(int i=0;i<k;i++)
{
nums.insert(nums.begin(), nums.back());
nums.pop_back();
}
}
};

然后换种方法:

class Solution {
public:
void rotate(vector<int>& nums, int k) {
int n=nums.size();
vector<int> v(n);
for(int i=n-k,j=0;i<n-1,j<k-1;i++,j++){
v[j]=nums[i];
}
for(int i=0,j=k;i<n-k-1,j<n-1;i++,j++){
v[j]=nums[i];
}
nums=v;
}
};

出错:Last executed input:[1,2,3,4,5,6], 11  

因为没有考虑到当k大于n的情况,所以需要改进:

class Solution {
public:
void rotate(vector<int>& nums, int k) {
int n = nums.size();
vector<int> rot(n);
for(int i = 0; i < n; i++) {
if((i + k) < n) rot[i + k] = nums[i];
if((i + k) >= n) {
rot[(i + k)%n] = nums[i];
}
}
nums = rot;
}
};

c语言

看看:

 void rotate(int* nums, int numsSize, int k) {
int i;
if(k > numsSize)
k -= numsSize;
int* temp = (int*)calloc(sizeof(int), numsSize);
for(i = 0; i < k; i++)
temp[i] = nums[numsSize - k + i];
for(; i < numsSize; i++)
temp[i] = nums[i - k]; for(i = 0; i < numsSize; i++)
nums[i] = temp[i];
}

 或:

void reverse(int *nums, int start, int end) {
int tmp;
while (start < end) {
tmp=nums[start];
nums[start]=nums[end];
nums[end]=tmp;
++start;
--end;
}
} void rotate(int* nums, int numsSize, int k) {
k=k%numsSize;
if (k==0) return;
reverse(nums,0,numsSize-k-1);
reverse(nums,numsSize-k,numsSize-1);
reverse(nums,0,numsSize-1);
}

  

 

  

 

leetcode:Rotate Array的更多相关文章

  1. [LeetCode] 189. Rotate Array 旋转数组

    Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...

  2. 【LeetCode】Rotate Array

    Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...

  3. Java [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 ...

  4. C#解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  ...

  5. LeetCode(67)-Rotate Array

    题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ar ...

  6. LeetCode之“数组”:Rotate Array

    题目链接 题目要求: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, ...

  7. LeetCode OJ:Rotate Array(倒置数组)

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  8. leetcode解题报告(20):Rotate Array

    描述 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the arr ...

  9. 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. matrix_last_acm_5

    password 123 A http://acm.hust.edu.cn/vjudge/contest/view.action?cid=97960#problem/A 题意:给国王生日可能区间[a, ...

  2. ios中webservice报文的拼接

    1.报文头需要密码验证的 - (void)sendAsynWebServiceRequest:(NSString *)nameSpace method:(NSString *)method reque ...

  3. 树链剖分 - BZOJ 1036: [ZJOI2008]树的统计Count

    这是树链剖分的入门题,也是我学树链剖分的第一题. 树链剖分:就是把树中和线段树联系起来,求(u,v)路径中权值的最大值和其路径的权值和. 入门blog:http://blog.sina.com.cn/ ...

  4. YARN的 AM与RM通信,申请资源分配过程

    AppMaster向RM请求资源 MRAppMaster :serviceinit // service to allocate containers from RM (if non-uber) or ...

  5. Yarn的服务库和事件库

    对于生命周期较长的对象,YARN采用了基于服务对象管理模型对其进行管理. 该模型有一下特点: 每个被服务化的对象都分为4个状态 任何服务状态变化都可以触发另外一些动作 可以通过组合方式对任意服务进行组 ...

  6. 为Form中的控件增加自适应功能 转

    创建一个基于Custom的类resizeable,并新建属性和方法程序,其说明如下: a) 新建属性: posiTyperList 可调整位置的控件类型列表sizeTypeList 可调整大小的控件类 ...

  7. tomcat 多开设置 需要需改的3个端口

    启动多tomcat需要需改的3个端口 我所用Tomcat服务器都为zip版,非安装版.以两个为例: 安装第二个Tomcat完成后,到安装目录下的conf子目录中打开server.xml文件,查找以下三 ...

  8. POJ 2185 Milking Grid (KMP,求最小覆盖子矩阵,好题)

    题意:给出一个大矩阵,求最小覆盖矩阵,大矩阵可由这个小矩阵拼成.(就如同拼磁砖,允许最后有残缺) 正确解法的参考链接:http://poj.org/showmessage?message_id=153 ...

  9. django转义safe

    “何谓转义?就是把html语言的关键字过滤掉.例如,<div>就是html的关键字,如果要在html页面上呈现<div>,其源代码就必须是<div> 默认情况下,d ...

  10. sql server 函数

    1.Fun_Split 分割字符串,形成返回表 CREATE FUNCTION [dbo].[Fun_Split] ( @Items VARCHAR(MAX), @SplitStr VARCHAR(M ...