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 ...
随机推荐
- CodeForces - 981D Bookshelves
Discription Mr Keks is a typical white-collar in Byteland. He has a bookshelf in his office with som ...
- POJ 3692 Kindergarten(最大独立集)
[题目链接] http://poj.org/problem?id=3692 [题目大意] 男生相互之间都认识,女生相互之间也都认识, 一些男生和一些女生相互之间也认识,求找出最多的人参加派对, 他们相 ...
- 微信小程序API·目录
网络 媒体 文件 数据缓存 位置 设备 界面 第三方平台 开放接口 数据 更新 多线程 监控 调试接口 日志
- Android中的动态字符串的处理
1.效果显示 2. MainAcitivity.java package com.example.app2; import android.support.v7.app.AppCompatActivi ...
- Git学习笔记(一) 安装及版本库介绍
安装Git 最早Git是在Linux上开发的,很长一段时间内,Git也只能在Linux和Unix系统上跑.不过,慢慢地有人把它移植到了Windows上.现在,Git可以在Linux.Unix.Mac和 ...
- 【js 正则表达式】记录所有在js中使用正则表达式的情况
说实话,对正则表达式有些许的畏惧感,之前的每次只要碰到需要正则表达式去匹配的情况,都会刻意的躲过或者直接从度娘处获取. 此时此刻,感觉到了某一个特定的点去触及她.但笔者对于正则表达式使用上的理解是这样 ...
- IOS开发使用委托delegate在不同窗口之间传递数据
IOS开发使用委托delegate在不同窗口之间传递数据是本文要介绍的内容,主要是来讲解如何使用委托delegate在不同窗口之间传递数据,具体内容来看详细内容.在IOS开发里两个UIView窗口之间 ...
- pip install py-stringsimjoin error: INCLUDE environment variable is empty
在用pip install py-stringsimjoin的时候报错error: INCLUDE environment variable is empty,后来在网上搜索下了说是需要下载安装VCF ...
- PHP测试用例-前言 1
前提知识准备 在学习本课程之前,你需要准备以下知识点: 掌握一般的PHP开发技能,使用面向对象的框架开发过三个月以上 会一些JS知识 了解http协议 拥有以下知识会学得更加顺利: 掌握PHPUnit ...
- Windows如何使用主题包
相信有很多朋友都遇到这样的问题 下载了主题却没有办法安装就是解压到C:\WINDOWS\Resources\Themes也是于事无补 没有任何反应 回到桌面 点击右键还是没有没有任何反应见图一 原因很 ...