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. 《Tensorflow技术解析与实战》第四章

    Tensorflow基础知识 Tensorflow设计理念 (1)将图的定义和图的运行完全分开,因此Tensorflow被认为是一个"符合主义"的库 (2)Tensorflow中涉 ...

  2. Latex排版全解(转)

    Latex排版全解 http://blog.csdn.net/langb2014/article/details/51354238

  3. Mybatis使用pageHelper步骤

    1.在pom.xml中添加如下依赖: <dependency> <groupId>com.github.pagehelper</groupId> <artif ...

  4. spring boot 以jar的方式启动常用shell脚本

    用spring boot框架做的项目,将第三方包全部打在jar里面,通过shell脚本启动和停止服务,常用的shell脚本模板如下: #!/bin/bashJAVA_OPTIONS_INITIAL=- ...

  5. Nginx -HTTP和反向代理服务器简单配置

    from:http://blog.sina.com.cn/s/blog_6b64b6a701011feh.html Nginx官方测试能够支持5万并发连接,实际生产环境中可支持2-4万并发连接数. 在 ...

  6. ZOJ 3958 Cooking Competition 【水】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3958 AC代码 #include <cstdio> ...

  7. C# 调用VS自带程序WebDev.WebServer40.EXE 源代码

    通过Process.Start启动,VS自带程序WebDev.WebServer40.EXE 在内网架设网站时,为安装IIS条件下用VS自带的小程序来测试效果非常不错! using System; u ...

  8. 建议47:使用logging记录日志信息

    # -*- coding:utf-8 -*- ''' Python中自带的logging 模块提供了日志功能,它将logger 的level 分为5 个级别 DEBUG 详细的信息,在追踪问题的时候使 ...

  9. @MarkFan 口语练习录音 20140401

    Hi,everybody 对于未来,我只梦想最好的情况, 并定下最踏实的计划,而绝不花时间在无谓的担心上, 因为我知道,只要把我对自己的承诺付诸实践, 我的未来将不会只是一个梦…… 这是引用考拉小巫的 ...

  10. 20145230《java程序设计》 第四次实验报告

    20145230实验4 Android开发基础 实验内容 基于Android Studio开发简单的Android应用并部署测试; 了解Android组件.布局管理器的使用: 掌握Android中事件 ...