<LeetCode OJ> 189. Rotate Array
189. Rotate Array
Submissions: 278176 Difficulty: Easy
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]
.
Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
第一种方法:
申请额外vector来处理,24ms
题目要求用三种方法
第一种:申请额外空间O(N),用vector直接处理
找规律:原数组中i位置的数据就是tmpnums中(i+k+len)/ len的数据
class Solution {
public:
void rotate(vector<int>& nums, int k) {
k=k%nums.size(); //k可能大于size
vector<int> tmpnums(nums.size());
for (int i=0;i<nums.size();i++)
tmpnums[(i+k+nums.size())%nums.size()]=nums[i];
nums=tmpnums;
}
};
另外一种方法:
技巧法(逆序),没有申请额外空间,24ms
另外一种:题目意思说能够原地处理
先前面nums.size()-k个数据逆序,接着整个数组总体逆序。最后将前k个数逆序
举例:4,3,2,1,5,6,7-------》7,6,5,1,2,3,4--------》5,6,7,1,2,3,4
class Solution {
public:
void rotate(vector<int>& nums, int k) {
k=k%nums.size();
for (int i=0;i<(nums.size()-k)/2;i++)
{
int tmp1=nums[i];
nums[i]=nums[nums.size()-k-1-i];
nums[nums.size()-k-1-i]=tmp1;
}
for (int i=0;i<nums.size()/2;i++)
{
int tmp1=nums[i];
nums[i]=nums[nums.size()-1-i];
nums[nums.size()-1-i]=tmp1;
}
for (int i=0;i<k/2;i++)
{
int tmp1=nums[i];
nums[i]=nums[k-1-i];
nums[k-1-i]=tmp1;
}
}
};
或者调用库函数来做(与上面的代码全然等价),24ms:
class Solution {
public:
void rotate(vector<int>& nums, int k) {
k=k%nums.size();
vector<int> ::iterator ite=nums.begin();
reverse(ite,ite+nums.size()-k);
reverse(ite,ite+nums.size());
reverse(ite,ite+k);
}
};
第三种方法:
循环左移或者右移(O(N*K)超时)
class Solution {
public:
void MoveRightByOne(vector<int>& nums) {
int temp = nums[ nums.size() - 1];
for (int i = nums.size() - 1; i >=1 ; --i) {
nums[i] = nums[i - 1];
}
nums[0] = temp;
} void MoveLeftByOne(vector<int>& nums) {
int temp = nums[0];
for (int i = 0; i < nums.size()-1 ; ++i) {
nums[i] = nums[i + 1];
}
nums[nums.size() - 1] = temp;
} void rotate(vector<int>& nums ,int k) {
k = k % nums.size();
if (k < nums.size()/2) {
for (int i = 0; i < k; ++i)
MoveRightByOne(nums);
} else {
for (int i = 0; i < nums.size()-k; ++i)
MoveLeftByOne(nums);
}
}
};
注:本博文为EbowTang原创,兴许可能继续更新本文。假设转载。请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/50449688
原作者博客:http://blog.csdn.net/ebowtang
<LeetCode OJ> 189. Rotate Array的更多相关文章
- LeetCode OJ 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】189. Rotate Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...
- 【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 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 ...
- 189. Rotate Array - LeetCode
Question 189. Rotate Array Solution 题目大意:数组中最后一个元素移到第一个,称动k次 思路:用笨方法,再复制一个数组 Java实现: public void rot ...
- 189. Rotate Array【easy】
189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...
- [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 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 ...
- Java for 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 ...
随机推荐
- ZigBee学习一 任务处理函数_ProcessEvent
ZigBee学习一 任务处理函数_ProcessEvent //任务处理函数UINT16 GenericApp_ProcessEvent( byte task_id, UINT16 events ){ ...
- ceoi2017 Building Bridges(build)
Building Bridges(build) 题目描述 A wide river has nn pillars of possibly different heights standing out ...
- webstorm卡顿
http://blog.csdn.net/qq673318522/article/details/50583831 http://www.xiaobai8.com/Blog/1000.html
- bzoj2438 杀人游戏 Tarjan强联通
[bzoj2438][中山市选2011]杀人游戏 Description 一位冷血的杀手潜入 Na-wiat,并假装成平民.警察希望能在 N 个人里面,查出谁是杀手.警察能够对每一个人进行查证,假如查 ...
- vmware虚拟机无法ping通主机,也无法联网
- JavaScript (JS) 函数补充 (含arguments、eval()、四种调用模式)
1. 程序异常 ① try-catch语法 测试异常 try-catch语法代码如下: try { 异常代码; try中可以承重异常代码, console.log(“try”) 出现异 ...
- 【转】linux之shfit
位置参数可以用shift命令左移.比如shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1.$2.$3丢弃,$0不移动.不带参数的shift命令相当于shift 1. 非常 ...
- ViewAnimator实现复杂的动画效果
咱们先看个原生的 AnimatorSet animatorSet = new AnimatorSet(); animatorSet.playTogether( ObjectAnimator.ofFlo ...
- App Store 审核指南(最新)
简介 App 正在改变世界,丰富人们的生活,并为像您一样的开发者提供前所未有的创新机会.因此,App Store 已成长为一个激动人心且充满活力的生态系统,正为数百万的开发者和超过十亿的用户提供服务. ...
- html table 使用总结
html中的table是一个历史相当悠久的标签,它能够很方便的实现数据的表格展示.虽然table是个很基础的标签,但是想用好还是对css相关知识有要求的. 由于table标签中自带的属性操作起来略为麻 ...