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. Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory 8 (转)

    Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory 8http://www.crifan.com/android_emulator_ ...

  2. Unity3D战争迷雾效果

    原地址:http://liweizhaolili.blog.163.com/blog/static/16230744201431835652233/ 最近一直都在做Flash相关的东西,很久没有空搞U ...

  3. poj 2311

    Cutting Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2844   Accepted: 1036 Desc ...

  4. 即时通讯UI-聊天界面(UITableView显示左右两人聊天)

    目录 1.创建UITableView对象并设置相关属性 2.创建cellModel模型 //枚举类型 typedef enum { ChatMessageFrom = ,//来自对方的消息 ChatM ...

  5. POJ 1573

    #include<iostream> #include<stdio.h> #define MAXN 15 using namespace std; char _m[MAXN][ ...

  6. android:scaleType属性

    android:scaleType是控制图片如何resized/moved来匹对ImageView的size. ImageView.ScaleType / android:scaleType值的意义区 ...

  7. HTTP协议header标头详解

    本文根据RFC2616(HTTP/1.1规范),参考 http://www.w3.org/Protocols/rfc2068/rfc2068 http://www.w3.org/Protocols/r ...

  8. BZOJ 1046: [HAOI2007]上升序列 LIS -dp

    1046: [HAOI2007]上升序列 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3438  Solved: 1171[Submit][Stat ...

  9. JDBC第一次学习

     JDBC(Java Data Base Connectivity,java数据库连接),由一些类和接口构成的API,它是J2SE的一部分,由java.sql,javax.sql包组成. 应用程序.J ...

  10. 如何使用JMeter来实现更大批量的并发的解决方案(即如何设置controller和Agent)

    http://www.testwo.com/blog/6373 近期在用JMeter进行负载测试的 时候,发现使用单台机器模拟测试超过比如500个进程的并发就有些力不从心或者说不能如实的反应实际情况, ...