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

将数组的内容倒置,看例子就知道是什么意思:

 class Solution {
public:
void rotate(vector<int>& nums, int k) {
if(k > nums.size()) k %= nums.size();//这里要注意,k的大小可能比size要大一点
vector<int> tmpVec1{nums.begin(), nums.begin() + (nums.size() - k)};
vector<int> tmpVec2{nums.begin() + (nums.size() - k), nums.end()};
int sz1 = tmpVec1.size();
for(int i = ; i < sz1; ++i){
tmpVec2.push_back(tmpVec1[i]);
}
nums = tmpVec2;
}
};

上面这个是数组拷贝的方法,不过速度比较慢一点,一开始用一个二重循环来解决,不过那样总是超时,所以又想了上面这个方法。

还有题目说了希望可以用到O(1)的额外空间。看了下别人写的:炒鸡简单啊

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

先把前面的部分反转,再将后面的部分反转,最后再做一次反转就可以了。

java接比较坑爹啊,还要自己手写反转函数,额,这一点没有STL好用了,可能只是我不知道罢了,代码如下:

 public class Solution {
public void rotate(int[] nums, int k) {
k = k % nums.length;
rev(nums, nums.length-k, nums.length - 1);
rev(nums, 0, nums.length-k-1);
rev(nums, 0, nums.length - 1);
} //自己要单独的写一个rev函数
public void rev(int [] arr, int start, int end){
int tmp = 0;
while(start < end){
tmp = arr[start];
arr[start] = arr[end];
arr[end] = tmp;
start++;
end--;
}
}
}

LeetCode OJ: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 翻转数组

    题意:给定一个数组,将该数组的后k位移动到前n-k位之前.(本题在编程珠玑中第二章有讲) 思路: 方法一:将后K位用vector容器装起来,再移动前n-k位到后面,再将容器内k位插到前面. class ...

  3. 【LeetCode】Rotate Array

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

  4. [LeetCode] 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 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. Rotate Array 旋转数组 JS 版本解法

    Given an array, rotate the array to the right by k steps, where k is non-negative. 给定一个数组,并且给定一个非负数的 ...

  7. leetcode: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. 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 ...

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

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

随机推荐

  1. 003-Spring 中的StreamUtils

    一.概述 StreamUtils是spring中用于处理流的类,是java.io包中inputStream和outputStream,不是java8中Steam.使用时仅依赖spring-core 二 ...

  2. 【AWS】订阅AWS论坛的RSS消息获取最新公告

    背景:AWS在遇到系统升级.系统故障等问题的时候,会在其官方论坛发布通知,并同步到RSS 前言:我们在项目中遇到几次AWS的RDS出现故障导致系统受影响,当系统故障时,我们的监控系统会发送一堆告警出来 ...

  3. 常用mysql导入导出数据的命令

    To export 导出指定db_name的数据: $ mysqldump -u [uname] -p[pass] db_name > db_backup.sql 导出整个库的数据: $ mys ...

  4. 学习OpenCV2——Mat之通道的理解

    本文详细介绍了opencv中涉及通道的知识,包括图像类型转换,通道合成分解,图像的显示. 来源:http://blog.csdn.net/GDFSG/article/details/50927257 ...

  5. 妙用php中的register_shutdown_function和fastcgi_finish_request

    前言 在php中又两个方法都是在请求快结束的时候执行.方法名分别是 register_shutdown_function和fastcgi_finish_request.虽然执行的时机差不多,但是功能和 ...

  6. 对称加密,API加密

    用于API加密,双方约定好signature_key对请求的参数进行处理 处理步骤如下 把请求的数据生成为key=>value的形式,然后拼接生成arg_key arg_key加上双方约定的si ...

  7. Squid 正向代理配置

    Squid 正向代理配置 1.删除主配置文件重写写入配置 rm -f /etc/squid/squid.conf 2.重新写入配置正向代理 vim /etc/squid/squid.conf # 监听 ...

  8. CSS伪元素实现的3D按钮

    在线演示 本地下载

  9. IMP导入小记

    1.创建表空间 create tablespace example_tablespace datafile 'e:\****.dbf' size 10m reuse autoextend on nex ...

  10. IEnumerable的一些基本方法

    在说明用法之后,先要弄点数据. class Product { public int ID { get; set; } public string Name { get; set; } public ...