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.

[show hint]

Hint:
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】的更多相关文章

  1. 26. Remove Duplicates from Sorted Array【easy】

    26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...

  2. 88. Merge Sorted Array【easy】

    88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...

  3. 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 ...

  4. 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 ...

  5. 88. Merge Sorted Array【Easy】【双指针-不用额外空间归并两个有序数组】

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...

  6. 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 ...

  7. 561. Array Partition I【easy】

    561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...

  8. 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 ...

  9. 27. Remove Element【easy】

    27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...

随机推荐

  1. 分析成绩 Exercise07_04

    import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:分析成绩 * */ public class Exercise07_04 ...

  2. ListView控件(下)简单适配器

    (一) 1.效果图 2.activiy_main.xml <?xml version="1.0" encoding="utf-8"?> <Li ...

  3. winform 窗体实现增删改查(CRUD)窗体基类模式

    参考博客下方:http://www.cnblogs.com/wuhuacong/archive/2010/05/31/1748579.html 对于一般常用到的编辑数据.新增数据窗体,分开了两个不同的 ...

  4. display:flex;多行多列布局学习

    从以前的table布局到现在的div布局,再到未来的flex布局,CSS重构方面对展示行和适应性的要求越来越高: 首先来比较一下布局方式的更新意义: table布局: 优点:1.兼容性好,ie6.ie ...

  5. Matlab与C++混合编程 编写独立外部应用程序时出现“无法定位序数3906于动态链接库LIBEAY32.dll上”错误

    出现“无法定位序数3906于动态链接库LIBEAY32.dll上”错误,这种错误一般是同名函数出现在两个不同的头文件中了. 笔者的这个错误是由于 #include "mat.h" ...

  6. 【java】File的使用:将字符串写出到本地文件,大小0kb的原因

    实现方法: 暂时写一种方法,将字符串写出到本地文件,以后可以补充更多种方法: public static void main(String[] args) { /** * ============== ...

  7. Yii2 使用十二 配合ajaxFileUpload 上传文件

    1.js $("input#upload").change(function () { $.ajaxFileUpload({ url: '/members/web-members- ...

  8. 【重点突破】—— 当better-scroll 遇见Vue

    前言:在学习黄轶老师的<Vue.js高仿饿了么外卖App>课程中接触到了better-scroll第三方JavaScript组件库,这是黄轶老师自己基于iscroll重写的库.这里结合黄轶 ...

  9. vue2 router-link to

    <template> <div> <nv-header></nv-header> <div class="artlist"&g ...

  10. Download FFmpeg

    Builds Static builds provide one self-contained .exe file for each program (ffmpeg, ffprobe, ffplay) ...