189. Rotate Array【easy】
189. Rotate Array【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.
Could you do it in-place with O(1) extra space?
Related problem: Reverse Words in a String II
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
解法一:
class Solution {
public:
void rotate(vector<int>& nums, int k) {
int len = nums.size();
if (len == || k % len == )
{
return;
}
int mid = len - k % len;
reverseArray(nums, , mid - );
reverseArray(nums, mid, len - );
reverseArray(nums, , len - );
}
void reverseArray(vector<int>& nums, int start, int end)
{
int s = start;
int e = end;
while (s <= e)
{
int temp = nums[s];
nums[s] = nums[e];
nums[e] = temp;
s++;
e--;
}
}
};
注意:
1、边界值检查,避免对0取余和长度不合法
2、先分别翻转,再总体翻转,注意下标
解法二:
class Solution
{
public:
void rotate(int nums[], int n, int k)
{
k = k%n; // Reverse the first n - k numbers.
// Index i (0 <= i < n - k) becomes n - k - i.
reverse(nums, nums + n - k); // Reverse tha last k numbers.
// Index n - k + i (0 <= i < k) becomes n - i.
reverse(nums + n - k, nums + n); // Reverse all the numbers.
// Index i (0 <= i < n - k) becomes n - (n - k - i) = i + k.
// Index n - k + i (0 <= i < k) becomes n - (n - i) = i.
reverse(nums, nums + n);
}
};
解法三:
public class Solution {
public void rotate(int[] nums, int k) {
k %= nums.length;
reverse(nums, , nums.length - );
reverse(nums, , k - );
reverse(nums, k, nums.length - );
}
public void reverse(int[] nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
}
先整体搞,再分开搞
189. Rotate Array【easy】的更多相关文章
- 26. Remove Duplicates from Sorted Array【easy】
26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...
- 88. Merge Sorted Array【easy】
88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
- 448. Find All Numbers Disappeared in an Array【easy】
448. Find All Numbers Disappeared in an Array[easy] Given an array of integers where 1 ≤ a[i] ≤ n (n ...
- 479. Second Max of Array【easy】
Find the second max number in a given array. Notice You can assume the array contains at least two n ...
- 88. Merge Sorted Array【Easy】【双指针-不用额外空间归并两个有序数组】
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...
- 167. Two Sum II - Input array is sorted【easy】
167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...
- 561. Array Partition I【easy】
561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...
- 121. Best Time to Buy and Sell Stock【easy】
121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...
- 27. Remove Element【easy】
27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...
随机推荐
- [BZOJ2527]Meteors
整体二分挺好玩的...学一发 这个询问显然是可以二分的,但每次都二分就会T爆,所以我们有了“整体”二分 每次处理一些询问,要求这些询问的答案一定在$[l,r]$中 先把$l$到$mid$的操作实施,那 ...
- 【神奇の做法】bzoj2456 mode
题解:http://www.tuicool.com/articles/BfQBzif #include<cstdio> using namespace std; int n,x,ans,t ...
- 零基础带你看Spring源码——IOC控制反转
本章开始来学习下Spring的源码,看看Spring框架最核心.最常用的功能是怎么实现的. 网上介绍Spring,说源码的文章,大多数都是生搬硬推,都是直接看来的观点换个描述就放出来.这并不能说有问题 ...
- 微服务之SpringCloud实战(二):SpringCloud Eureka服务治理
服务治理 SpringCloud Eureka是SpringCloud Netflix微服务套件的一部分,它基于Netflix Eureka做了二次封装,主要完成微服务的服务治理功能,SpringCl ...
- opencv在vc2010 express下环境搭建方法笔记+空白通用工程(已编译测试通过)(提供下载)
opencv在VC2010 express版本下的环境搭建可以参见下面的wiki,这里面讲的非常清楚. http://wiki.opencv.org.cn/index.php/VC_2010_Expr ...
- sql server博客
SQLSERVER MSDN论坛 SQLSERVER 补丁博客 SQLSERVER中国研发中心 微软亚太区数据库技术支持组官方博客 PAUL的SQLSKILL网站 sqlsaturday网站 sqls ...
- struts-2-spring-2-jpa-ajax
http://struts.apache.org/docs/struts-2-spring-2-jpa-ajax.html
- Matlab中向量场的绘制
% quiver(x,y,u,v) % x,y是包含坐标位置的矩阵,而u和v则是包含偏导数的矩阵. % 例如绘制f(x,y)=y-3x-2x^2-3xy-3y^2的方法: % 先用gradient函数 ...
- css活用,评分点击星星
1.字符图集 2.css样式 .cleanfloat::after{display: block; clear: both; content:""; visibility: hid ...
- 关于constraint 的disable和enable
建立主外键的constraint create table emp1(emp_no number(2) constraint emp_emp_no_pk primary key,ename varch ...