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 ...
随机推荐
- UOJ Rounds
UOJ Test Round #1 T1:数字比大小的本质是按(长度,字典序)比大小. T2:首先发现单调性,二分答案,用堆模拟,$O(n\log^2 n)$. 第二个log已经没有什么可优化的了,但 ...
- 【动态规划】bzoj1649 [Usaco2006 Dec]Cow Roller Coaster
很像背包. 这种在一个数轴上进行操作的题常常需要对区间排序. f[i][j]表示距离到i时,花费为j时的权值之和. f[x[i]+l[i]][j+c[i]]=max{f[x[i]][j]+w[i]}( ...
- 【计算几何】【极角序】【二分】bzoj1914 [Usaco2010 OPen]Triangle Counting 数三角形
极角排序后枚举每个点,计算其与原点连线的左侧的半平面内的点与其组成的三角形数(二分/尺取),这些都不是黄金三角形. 补集转化,用平面内所有三角形的个数(C(n,3))减去这些即可. 精度很宽松,几乎不 ...
- 解决ubuntu系统中wireshark:Couldn't run /usr/bin/dumpcap in child process: Permission denied的问题
ubuntu系统运行WIreshark的时候,出现如下错误: Couldn't run /usr/bin/dumpcap in child process: Permission denied 解决办 ...
- 9.1(java学习笔记)正则表达式
一.正则表达式 1.1正则表达式 正则表达式是描述一种规则,通过这个规则可以匹配到一类字符串. 2.1正则语法 2.1.1普通字符:字母.数字.下划线.汉字以及没有特殊意义的符号都是普通字符. 正则表 ...
- 指定等级 Exercise07_01
import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:指定等级 * */ public class Exercise07_01 ...
- (转)unity web 缓存解决方案
unity web 缓存解决方案 官方发布 web版限制五十M缓存,根据自己的经验绕了过去,解决了缓存的问题.带工程,带源代码.由于本人的水平也有限,是用JS来解决的,如果你还是没有头绪,可以购买来试 ...
- linux下安装python2.7.5和MYSQLdb
由于开发的python web 扫描器需要在python2.7.5以及需要MYSQLdb这个库的支持,在此做一个记录,避免更换到新环境时的学习成本. 一.安装MYSQL 1.yum install m ...
- SQLSERVER中汉字提取首字母的拼音函数的实现
--创建一个汉字提取首字母的函数--还存在一点小小的问题(符号?)create function hs(@a varchar(1000)='')returns varchar(1000)asbegin ...
- SQL CTE 递归分割以逗号分隔的字符串
)) INSERT INTO @t SELECT 'AAA,BBB,CCC' SELECT * FROM @t ;WITH mycte AS ( ,mend,num FROM @t UNION ALL ...