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. HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. HDU1070Milk

     Milk Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description ...

  3. <string>和<string.h>的区别

    转自:http://blog.csdn.net/houjixin/article/details/8648969 在C++开发过程中经常会遇到两个比较容易混淆的头文件引用#include<str ...

  4. CSS兼容问题大全

    1.chorme 最小字体的兼容性. 问题描述:ff和IE最小字体可设置为1px,可是chorme中文版最小字体是12px,小于12px的字体全部显示为12px.解决方案:chorme支持CSS3的, ...

  5. 如何做好一名DBA【转】

    我一直有一个观点:程序是暂时的,而数据是永恒的.所以我一直都认为数据的重要性在很多企业中都远远高于应用程序,在多年的工作实践中努力做好DBA的工作.而要做好一名DBA,必须要清楚作为一名DBA的职责. ...

  6. 【译】C++工程师需要掌握的10个C++11特性

    原文标题:Ten C++11 Features Every C++ Developer Should Use 原文作者:Marius Bancila 原文地址:codeproject 备注:非直译,带 ...

  7. 企业级Java应用最重要的4个性能指标

    应用性能管理(APM)是一种即时监控以实现对应用程序性能管理和故障管理的系统化解决方案.目前主要指对企业的关键业务应用进行监测.优化,最终达到提高企业应用的可靠性和质量,保证用户得到良好的服务,降低I ...

  8. 查看w3wp进程占用的内存及.NET内存泄露,死锁分析--转载

    一 基础知识 在分析之前,先上一张图: 从上面可以看到,这个w3wp进程占用了376M内存,启动了54个线程. 在使用windbg查看之前,看到的进程含有 *32 字样,意思是在64位机器上已32位方 ...

  9. vc++ 获取当前用户名

    #include<afxwin.h> #include <stdio.h> int main(void) { char userName[MAX_PATH]; unsigned ...

  10. (转)Learning to Rank for IR的评价指标—MAP,NDCG,MRR

    转自:http://www.cnblogs.com/eyeszjwang/articles/2368087.html MAP(Mean Average Precision):单个主题的平均准确率是每篇 ...