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 ...
随机推荐
- Latex排版全解(转)
Latex排版全解 http://blog.csdn.net/langb2014/article/details/51354238
- 脉冲神经网络及有监督学习算法Tempotron
接下来一段时间开启脉冲神经网络模型的探索之旅.脉冲神经网络有更强的生物学基础,尽可能地模拟生物神经元之间的连接和通信方式.其潜在能力较强,值得踏进一步探索. 构建脉冲神经网络模型,至少需要考虑三点:1 ...
- 20170405-STO库存转储单
1.工厂间转储: (1)MB1B 移动类型 301 工厂到工厂(一步)转账,->简单明了一步转储过账后会产生 GR,MITA增加了,MIZH减少了,MB03, **会产生 GR,如果俩工厂 标准 ...
- 剑指offer 面试28题
面试28题: 题目:对称的二叉树题: 请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的 解题思路: 可以定义一种遍历算法,先遍历右子节点再遍 ...
- python常用模块——time模块
参考博客:http://blog.csdn.net/SeeTheWorld518/article/details/48314501 http://www.jb51.net/article/49325. ...
- python——异常
一.什么是异常 1.错误 从软件方面来说,错误是语法或是逻辑上的.错误是语法或是逻辑上的. 语法错误指示软件的结构上有错误,导致不能被解释器解释或编译器无法编译.这些些错误必须在程序执行前纠正. 当程 ...
- Android:日常学习笔记(2)——分析第一个Android应用程序
Android:日常学习笔记(2)——分析第一个Android应用程序 Android项目结构 整体目录结构分析 说明: 除了APP目录外,其他目录都是自动生成的.APP目录的下的内容才是我们的工作重 ...
- 03 Spring框架 bean的属性以及bean前处理和bean后处理
整理了一下之前学习spring框架时候的一点笔记.如有错误欢迎指正,不喜勿喷. 上一节我们给出了三个小demo,具体的流程是这样的: 1.首先在aplicationContext.xml中添加< ...
- ajax json 异步请求
function ajaxTest(){ if (true) { $.ajaxSettings.async = false; var dataJson; $.getJSON("/univer ...
- android各种组件的监听器
<一>Spinner(旋转按钮或下拉列表):设置监听器为:setOnItemSelectedListener 设置动画效果为:setOnTouchListener ...