LeetCode OJ:Rotate Array(倒置数组)
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(倒置数组)的更多相关文章
- [LeetCode] 189. Rotate Array 旋转数组
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- LeetCode Rotate Array 翻转数组
题意:给定一个数组,将该数组的后k位移动到前n-k位之前.(本题在编程珠玑中第二章有讲) 思路: 方法一:将后K位用vector容器装起来,再移动前n-k位到后面,再将容器内k位插到前面. class ...
- 【LeetCode】Rotate Array
Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...
- [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 ...
- 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 ...
- Rotate Array 旋转数组 JS 版本解法
Given an array, rotate the array to the right by k steps, where k is non-negative. 给定一个数组,并且给定一个非负数的 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Linux三种网络
Host-Only 桥接
- GPS USB驱动串口被占用
1.一般是装了错误的驱动,显示如下 2.实际装好应该是显示的 3.驱动选择,先卸载了上面的virtual驱动,安装下面箭头指向的驱动 这里的卸载很重要,先点设备管理器的--查看--显示隐藏设备, 然后 ...
- More about Parameter Passing in Python(Mainly about list)
我之前写了一篇关于Python参数传递(http://www.cnblogs.com/lxw0109/p/python_parameter_passing.html)的博客, 写完之后,我发现我在使用 ...
- asp.net(c#)中String.Empty、NULL、"" 三者到底有啥区别和联系?
开门见山,首先看下面代码,你认为结果分别是什么? string str = string.Empty; string str1 = ""; string str2 = null; ...
- 曾经跳过的坑----js截取字符串substr与substring 和 trim
不废话直接代码.自己理解...... > "abcdefg".substring(1,6)> "bcdef"> "abcdefg&q ...
- loadrunner之脚本篇——录制方式HTML-based和URL-based Script
A. HTML-based Script 针对 Web (HTTP/HTML)虚拟用户的缺省录制级别.它指示VuGen录制当前web页面上下文中的HTML action.录制会话期间并不录制所有资 ...
- Docker容器技术-自动化部署
一.用Chef自动化部署Docker 1.为什么需要自动化部署? Docker引擎需要配置很多参数(cgroups.内存.CPU.文件系统等) 识别Docker容器运行在哪个宿主机上 耗时且容易出错, ...
- awk中的常用关于处理字符串的函数
1.替换字符串中的某一部分. 函数:gensub(/rexpr/,"replace","g","string"),gensub返回一个新的字 ...
- awk的getline命令
原文出处:生活费的博客,http://www.cnblogs.com/276815076/archive/2011/12/05/2276625.html#undefined 两种情况:getline ...
- 大话设计模式之PHP篇 - 简单工厂模式
假设有一道编程题:输入两个数字和运算符,然后得到运算结果.非常简单的一道题目,通常的实现代码如下: <?php Function Operation($val1, $val2, $operate ...