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. Ubuntu 12.04 SSH 安装

    By default Ubuntu Desktop OS comes with ssh clientpackage. It does not include ssh server package wh ...

  2. Windows Services windows域账户管理

    windows  域账户管理 一.什么是域账户: 域账户是域是网络对象的分组.例如:用户.组和计算机.域中所有的对象都存储在 Active Directory 下.Active Directory 可 ...

  3. 短信发送AZDG加密算法

    public static string passport_encrypt(string txt, string key)         {             //   使用随机数发生器产生  ...

  4. 《c程序设计语言》读书笔记--大写转小写

    #include <stdio.h> #include <stdlib.h> #include <string.h> int aoti(char c) { if(c ...

  5. poj -1065 Wooden Sticks (贪心or dp)

    http://poj.org/problem?id=1065 题意比较简单,有n跟木棍,事先知道每根木棍的长度和宽度,这些木棍需要送去加工,第一根木棍需要一分钟的生产时间,如果当前木棍的长度跟宽度 都 ...

  6. 【HDOJ】5657 CA Loves Math

    1. 题目描述对于给定的$a, n, mod, a \in [2,11], n \in [0, 10^9], mod \in [1, 10^9]$求出在$[1, a^n]$内的所有$a$进制下的数并且 ...

  7. Hosting Your Own NuGet Feeds

    Hosting Your Own NuGet Feeds Hosting the NuGet Gallery Locally in IIS https://github.com/NuGet/NuGet ...

  8. 总结Selenium自动化测试方法(二)测试环境搭建

    (接上期内容) 二.测试环境搭建 1.安装python 现在python3.0比python2.0多了一些改进的功能(详见http://zhidao.baidu.com/link?url=3sT1g7 ...

  9. js中的json对象详细介绍

    JSON一种简单的数据格式,比xml更轻巧,在JavaScript中处理JSON数据不需要任何特殊的API或工具包,下面为大家详细介绍下js中的json对象, 1.JSON(JavaScript Ob ...

  10. Kafka安装与实验

    接上面一篇文章: http://www.cnblogs.com/charlesblc/p/6038112.html 主要参考这篇文章: http://www.open-open.com/lib/vie ...