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?

Related problem: Reverse Words in a String II

Solution 1:reverse[1,2,3,4]=[4,3,2,1], reverse[5,6,7]=[7,6,5], reverse[4,3,2,1,7,6,5]=[5,6,7,1,2,3,4]

 #include<algorithm>
class Solution {
public:
void rotate(vector<int>& nums, int k) { //runtime:24ms
int n=nums.size();
k %= n;
reverse(nums.begin(),nums.begin()+n-k);
reverse(nums.begin()+n-k,nums.end());
reverse(nums.begin(),nums.end());
}
};

Solution 2:超时

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

Solution 3:

 void rotate(int nums[], int n, int k) {
k = k % n;
if (k == ) return;
int *temp = new int[n];
memcpy(temp, nums+(n-k), sizeof(int)*k);
memcpy(temp+k, nums, sizeof(int)*(n-k));
memcpy(nums, temp, sizeof(int)*n);
delete[] temp;
}

【LeetCode】189 - Rotate Array的更多相关文章

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

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

  2. 【easy】189. Rotate Array

    题目标签:Array 题目给了我们一个数组 和 k. 让我们 旋转数组 k 次. 方法一: 这里有一个很巧妙的方法: 利用数组的length - k 把数组 分为两半: reverse 左边和右边的数 ...

  3. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  4. 【LeetCode】61. Rotate List 解题报告(Python)

    [LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  5. 【leetcode】905. Sort Array By Parity

    题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...

  6. 【LeetCode】48. Rotate Image

    Difficulty:medium  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/rotate-image/ ...

  7. 【leetcode❤python】 189. Rotate Array

    #-*- coding: UTF-8 -*-#由于题目要求不返回任何值,修改原始列表,#因此不能直接将新生成的结果赋值给nums,这样只是将变量指向新的列表,原列表并没有修改.#需要将新生成的结果赋予 ...

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

  9. 【LeetCode】1020. Partition Array Into Three Parts With Equal Sum 解题报告(Python)

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

随机推荐

  1. hive-0.12升级成hive 0.13.1

    安装了0.12之后,听说0.13.1有许多新的特性,包括永久函数,所以想更新成0.13版的(元数据放在mysql中) 2014年8月5日实验成功 hive0.13.1的新特性 新特性详见 http:/ ...

  2. android ProgressBar 样式讲解

    转载自:eoe社区,可惜没找到源地址... 多式样ProgressBar 普通圆形ProgressBar 该类型进度条也就是一个表示运转的过程,例如发送短信,连接网络等等,表示一个过程正在执行中. 一 ...

  3. ISE MAP报错: Unsupported programming for BSCAN block and JTAG_CHAIN attribute value 1的解决方法

    2014-04-16 17:35:30 ISE MAP报错: Unsupported programming for BSCAN block and JTAG_CHAIN attribute valu ...

  4. PostgreSQL删除表

    PostgreSQL的DROP TABLE语句是用来删除表定义及其所有相关的数据表的索引,规则,触发器和约束. 必须使用此命令时要小心,因为一旦一个表被删除表中提供的所有信息也将被永远失去了. 语法: ...

  5. 理解Java的封装与接口

    1.封装,即保留有限的外部接口(interface),隐藏具体实施细节. 2.封装在生活中很常见.比如下面是一个充电电筒: 一个用户即使不看说明书,也可以猜到这个电筒的操作: 开关和充电.这个电筒用一 ...

  6. number-of-boomerangs

    https://leetcode.com/problems/number-of-boomerangs/ package com.company; import java.util.*; class S ...

  7. Codeforces 445 A DZY Loves Chessboard【DFS】

    题意:给出n*m的棋盘,在‘.’处放上B或者W,最后要求所有的B和W都不相邻 先把棋盘的点转化成‘B’,再搜,如果它的四周存在‘B’,则将它变成'W' 一直挂在第五个数据的原因是,没有dfs(nx,n ...

  8. 高性能WEB开发之Web性能测试工具推荐

    Firebug: Firebug 是firefox中最为经典的开发工具,可以监控请求头,响应头,显示资源加载瀑布图: HttpWatch: httpwatch 功能类似firebug,可以监控请求头, ...

  9. jstl表达式

    JSTL标签库 1.什么是JSTL JSTL是apache对EL表达式的拓展(也就是说JSTL依赖EL),JSTL是标签语言!JSTL标签使用以来非常方便, 它与JSP动作标签一样,只不过它不是JSP ...

  10. (转)beanUtil接口和类(有空的时候去看,到时候删除这个说明)

    Jakarta Commons项目提供了相当丰富的API,我们之前了解到的Commons Lang只是众多API的比较核心的一小部分而已.Commons下面还有相当数量的子项目,用于解决各种各样不同方 ...