#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,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]

Hint:
Could you do it in-place with O(1) extra space?

题解:最容易想到的是右移k次,用最容易想到的方法,结果是超时,时间复杂度是O(kn).

class Solution {
public:
void rotate(vector<int>& nums, int k) { int n=nums.size(); while(k--)
{
rightShift(nums);
}
}
void rightShift(vector<int>& nums)
{
int temp=;
temp=nums[nums.size()-];
for(int i=nums.size()-;i>;i--)
{
nums[i]=nums[i-];
}
nums[]=temp;
}
};

只好换三步反转法来做,时间复杂度是O(n),空间复杂度是O(1)

class Solution {
public:
void rotate(vector<int>& nums, int k) {
int n=nums.size();
k=k%n;
if(k==)
return ; reverseString(nums,,n-k-);
reverseString(nums,n-k,n-);
reverseString(nums,,n-);
}
void reverseString(vector<int>& nums,int from,int to)
{
while(from<to)
{
int temp=nums[from];
nums[from++]=nums[to];
nums[to--]=temp;
}
}
};

Leetcode-189 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 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  ...

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

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

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

  6. Leetcode 189 Rotate Array stl

    题意:将数组旋转k次,如将数组[1,2,3,4,5]旋转1次得到[2,3,4,5,1],将数组[1,2,3,4,5]旋转2次得到[3,4,5,1,2]..... 本质是将数组分成两部分a1,a2,.. ...

  7. &lt;LeetCode OJ&gt; 189. Rotate Array

    189. Rotate Array Total Accepted: 55073 Total Submissions: 278176 Difficulty: Easy Rotate an array o ...

  8. 189. Rotate Array - LeetCode

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

  9. 189. Rotate Array【easy】

    189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...

  10. 【LeetCode】Rotate Array

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

随机推荐

  1. IQKeyboardManager在某个页面取消键盘上面的Toolbar

    使用IQKeyboardManager的时候,如果想在某个页面取消键盘上面的Toolbar,请使用如下方法,按控制器去操作 // 取消IQKeyboardManager Toolbar [[IQKey ...

  2. workerman 的回调函数

    接下来,记录一下workerman 的回调函数 <?php /** * Created by PhpStorm. * User: zeopean * Date: 2016-08-26 * Tim ...

  3. Sql 中text类型字段判断是否为空

    用 len关键字,字段=''会报错:数据类型 text 和 varchar 在 equal to 运算符中不兼容. 正确方法: 1. 字段 is null 2. datalength(字段)=0 注: ...

  4. 近期oepnfire工作总结.

    1.优化订阅好友流程,增加验证消息2.优化好友查询模块,实现对扩展字段的查询.如批量匹配通讯录.3.实现webservice接口方式消息推送功能,供其他系统调用.4.实现花名册版本(XEP-237), ...

  5. USB协议(1)

    今天开始学习USB协议,精挑细选,我决定使用<圈圈教你玩USB>这本书,并且参考网友翻译的<USB2.0中文协议>. 这两本书都可以在ishare.sina.com.cn 即新 ...

  6. 【java】:通用接口

    电商接口 京东获取单个商品价格接口: http://p.3.cn/prices/mgets?skuIds=J_商品ID&type=1 用例 ps:商品ID这么获取:http://item.jd ...

  7. windows server 2008 - 创建域和本机用户

    /* * ===================================================================================== * Filenam ...

  8. 与jquery serializeArray()一起使用的函数,主要来方便提交表单

    <script> $(function() { $("#butsubmit").click(function() { var data = convertArray($ ...

  9. C++ map使用(基于RBTree)

    一.insert ◦1)用insert函数插入pair数据 ◦map<int, string> mapStudent; ◦mapStudent.insert(pair<int, st ...

  10. Hibernate Tools for Eclips Plugins

    1. 安装: Help -> Install new software -> Add ->Achieve -> 找到安装包 -> OK 2. 创建映射: