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. 20170401 ABAP调用CIS webservice

    问题: SAP  abap SRM java  调webservice 不通, CIS java  这边的webservice 可以通, WHY? key:请求头,系统框架的问题, LF:因为请求头的 ...

  2. 我的第一个Python小程序

    猜年龄,如果大了提示小点,如果小了,提示大点 涉及的知识点: 1.变量 2.注释 3.接收交互式的输入 4.类型转换 5.while循环 6.if..elif..else多条件分支语句 # Autho ...

  3. HAProxy的访问控制

    HAProxy的ACL用于实现基于请求报文首部.响应报文的内容或其他的环境状态信息来做出转发决策,这大大增强了其配置弹性,其配置法则通常分为两步,首先去定义ACL,即定义一个测试条件,而后在条件得到满 ...

  4. 安装和使用jupyter

    安装 pip install jupyter 使用 jupyter notebook

  5. Http1.0和Http1.1的主要区别

    1.HTTP 1.1支持长连接(PersistentConnection)和请求的流水线(Pipelining)处理 HTTP 1.0规定浏览器与服务器只保持短暂的连接,浏览器的每次请求都需要与服务器 ...

  6. Win8.1和office2013使用电话激活步骤

    Win8.1和office2013使用电话激活步骤: 先从Win8.1贴吧的最后几个回复中找到几个密钥,最后的通常是最新的,然后输入到Win8.1中,会提示你密钥无效,如果有效你就走了狗屎运了,无效的 ...

  7. Linux基础系列:常用命令(4)_系统监控

    1. 系统监视和进程控制工具—top和free 1) top命令的功能:top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. 2) ...

  8. HttpUtils工具类

    HttpUtils工具类 package net.hs.itn.teng.common.util; import java.io.IOException; import java.io.Unsuppo ...

  9. 发现一个小坑的地方,unity的协程,想要停止,必须以字符串启动

    今天想要停止一个协成,发现调用 StopCoroutine(ShowDebug()); 竟然不管用,后来看了文档才知道,原来想要停止协成,必须用字符启动协程 StartCoroutine(" ...

  10. RpcException:No provider available for remote service异常

    出现RpcException:No provider available for remote service异常,表示没有可用的服务提供者. 解决思路: 1.检查连接的注册中心是否正确 2.到注册中 ...