Leetcode-189 Rotate Array
#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 [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.
Could you do it in-place with O(1) extra space?
题解:最容易想到的是右移k次,用最容易想到的方法,结果是超时,时间复杂度是O(kn).
class Solution {
public:
void rotate(vector<int>& nums, int k) { int n=nums.size(); while(k--)
{
rightShift(nums);
}
}
void rightShift(vector<int>& nums)
{
int temp=;
temp=nums[nums.size()-];
for(int i=nums.size()-;i>;i--)
{
nums[i]=nums[i-];
}
nums[]=temp;
}
};
只好换三步反转法来做,时间复杂度是O(n),空间复杂度是O(1)
class Solution {
public:
void rotate(vector<int>& nums, int k) {
int n=nums.size();
k=k%n;
if(k==)
return ; reverseString(nums,,n-k-);
reverseString(nums,n-k,n-);
reverseString(nums,,n-);
}
void reverseString(vector<int>& nums,int from,int to)
{
while(from<to)
{
int temp=nums[from];
nums[from++]=nums[to];
nums[to--]=temp;
}
}
};
Leetcode-189 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 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 ...
- 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 189 Rotate Array stl
题意:将数组旋转k次,如将数组[1,2,3,4,5]旋转1次得到[2,3,4,5,1],将数组[1,2,3,4,5]旋转2次得到[3,4,5,1,2]..... 本质是将数组分成两部分a1,a2,.. ...
- <LeetCode OJ> 189. Rotate Array
189. Rotate Array Total Accepted: 55073 Total Submissions: 278176 Difficulty: Easy Rotate an array o ...
- 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】Rotate Array
Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...
随机推荐
- 6.HotSpot垃圾收集器
HotSpot JVM收集器 上面有7中收集器,分为两块,上面为新生代收集器,下面是老年代收集器.如果两个收集器之间存在连线,就说明它们可以搭配使用. 并发和并行 先解释下什么是垃圾收集器的上下文语境 ...
- 使用Resource Owner Password Credentials Grant授权发放Token
对应的应用场景是:为自家的网站开发手机 App(非第三方 App),只需用户在 App 上登录,无需用户对 App 所能访问的数据进行授权. 客户端获取Token: public string Get ...
- ListView和ScrollView冲突
当ListView放在ScrollView中的时候,无论你设置高度为 match_parent(填充父窗体)和wrap_content(包裹内容)都只显示一行,这是你把ListView放在Linear ...
- cinder backup
cinder 备份提供的驱动服务有: cinder/backup/drivers/ceph.py:def get_backup_driver(context): cinder/backup/drive ...
- DP专题——括号序列
毕竟是个渣,写完一遍之后又按LRJ的写了一遍,再写了一遍递归版,最终加上输出解部分 括号序列 定义如下规则序列(字符串): 空序列是规则序列: 如果S是规则序列,那么(S)和[S]也是规则序列: 如果 ...
- 去哪儿网mysql语法审核工具Inception正式开源
Inception不仅仅是一个自动化审核工具,同时还具备执行SQL,并且生成对影响数据的回滚语句(类似于闪回的功能),这样一条龙便捷服务的工具.
- ORACLE 學習筆記
proc 里的 commit等于提交就是你做了insert或者update后,commit后才是真正修改或者插入了数据库中 如果不提交的话,那么这个表就被锁了 CURSOR MYCURSOR is ...
- Extjs的GridPanel的RowExpander的扩展
对Extjs的grid使用,有时候单单使用其中的某些组.或某些行是远远不够的,还需要对行进行一些扩展,如:与filters相似的row扩展控件,如下 这个控件,我也是从网上找的小例子,按照其内部的某些 ...
- NSMutableAttributedString/NSAttributedString 富文本设置
今天在做项目的过程中,我们的设计师想要一种字体四周都带阴影的效果,但是我们平时使用的setShadowColor 和setShadowOffset是达不到这种效果,setShadowOffset 只能 ...
- 享受LINQ:判断一组文字是否在字符串中同时出现的最简单方法
需求是这样的:不允许在一个字符串中同时出现"博", "客", "园", "团", "队"这5个文字. ...