189. Rotate Array

Total Accepted: 55073 Total
Submissions: 278176 Difficulty: 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 different ways to solve this problem.

第一种方法:

申请额外vector来处理,24ms

题目要求用三种方法

第一种:申请额外空间O(N),用vector直接处理

找规律:原数组中i位置的数据就是tmpnums中(i+k+len)/ len的数据

class Solution {
public:
void rotate(vector<int>& nums, int k) {
k=k%nums.size(); //k可能大于size
vector<int> tmpnums(nums.size());
for (int i=0;i<nums.size();i++)
tmpnums[(i+k+nums.size())%nums.size()]=nums[i];
nums=tmpnums;
}
};

另外一种方法:

技巧法(逆序),没有申请额外空间,24ms

另外一种:题目意思说能够原地处理

先前面nums.size()-k个数据逆序,接着整个数组总体逆序。最后将前k个数逆序

举例:4,3,2,1,5,6,7-------》7,6,5,1,2,3,4--------》5,6,7,1,2,3,4

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

或者调用库函数来做(与上面的代码全然等价),24ms:

class Solution {
public:
void rotate(vector<int>& nums, int k) {
k=k%nums.size();
vector<int> ::iterator ite=nums.begin();
reverse(ite,ite+nums.size()-k);
reverse(ite,ite+nums.size());
reverse(ite,ite+k);
}
};

第三种方法:

循环左移或者右移(O(N*K)超时)

class Solution {
public:
void MoveRightByOne(vector<int>& nums) {
int temp = nums[ nums.size() - 1];
for (int i = nums.size() - 1; i >=1 ; --i) {
nums[i] = nums[i - 1];
}
nums[0] = temp;
} void MoveLeftByOne(vector<int>& nums) {
int temp = nums[0];
for (int i = 0; i < nums.size()-1 ; ++i) {
nums[i] = nums[i + 1];
}
nums[nums.size() - 1] = temp;
} void rotate(vector<int>& nums ,int k) {
k = k % nums.size();
if (k < nums.size()/2) {
for (int i = 0; i < k; ++i)
MoveRightByOne(nums);
} else {
for (int i = 0; i < nums.size()-k; ++i)
MoveLeftByOne(nums);
}
}
};

注:本博文为EbowTang原创,兴许可能继续更新本文。假设转载。请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50449688

原作者博客:http://blog.csdn.net/ebowtang

&lt;LeetCode OJ&gt; 189. Rotate Array的更多相关文章

  1. LeetCode OJ 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  ...

  2. 【LeetCode】189. Rotate Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...

  3. 【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  ...

  4. 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  ...

  5. 189. Rotate Array - LeetCode

    Question 189. Rotate Array Solution 题目大意:数组中最后一个元素移到第一个,称动k次 思路:用笨方法,再复制一个数组 Java实现: public void rot ...

  6. 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. [LeetCode] 189. Rotate Array 旋转数组

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

  8. 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  ...

  9. Java for 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. BZOJ 3224 普通平衡树(Treap模板题)

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 14301  Solved: 6208 [Submit][ ...

  2. linux编程学习

    linux编程学习 工具篇 “公欲善其事,必先利其器”.编程是一门实践性很强的工作,在你以后的学习或工作中,你将常常会与以下工具打交道, 下面列出学习 C 语言编程常常用到的软件和工具. (一)操作系 ...

  3. 刷题总结——飞飞侠(bzoj2143 最短路)

    题目: Description 飞飞国是一个传说中的国度,国家的居民叫做飞飞侠.飞飞国是一个N×M的矩形方阵,每个格子代表一个街区.然而飞飞国是没有交通工具的.飞飞侠完全靠地面的弹射装置来移动.每个街 ...

  4. vue2+nodejs+mongodb搭建移动端网站

    从零开始一步步搭建移动端网站,持续更新,github代码如下,因为放了视频文件,下载可能有点慢 https://github.com/lanleilin/myHomepage 前端采用Vue2+vue ...

  5. 【CF1015E】Stars Drawing(贪心)

    题意:给定一个n×m大小的字符矩阵,仅由‘.’和‘*’组成,询问这个图可否划分为一些由‘*’组成的十字形状,这些十字之间可以有重叠, 如果存在方案则输出每个十字中心坐标与边长度,无解输出-1 n,m& ...

  6. 【Educational Codeforces Round 53 (Rated for Div. 2)】

    A:https://www.cnblogs.com/myx12345/p/9853775.html B:https://www.cnblogs.com/myx12345/p/9853779.html ...

  7. 【Windows Message】MFC 通过F5,刷新桌面

    //通过F5,刷新桌面 HWND hWndProgram = ::FindWindow( _T("Progman"), NULL); HWND hWndDefView = ::Fi ...

  8. android的布局-----FrameLayout(帧布局)

    (-)帧布局简介 帧布局容器为每个加入的其中的组件创建一个空白的区域称为一帧每个子组件占据一帧,这些帧都会根据gravity的属性执行自动对齐 (二)属性 foreground:这是帧布局的前景图像 ...

  9. C语言集锦(三)Direct3D和GDI+的例子

    0.前言 有些时候你可能想了解,如何用纯C语言来写Direct3D和GDI+的Demo.注意,下面的Direct3D例子不适用于TCC编译器,GDI+的例子是可以的. 1.Direct3D C语言的例 ...

  10. hdu 4522(图论,构图)

    湫湫系列故事——过年回家 Time Limit: 500/200 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total ...