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.

解法一:

  1. class Solution {
  2. public:
  3. void rotate(vector<int>& nums, int k) {
  4. int len = nums.size();
  5.  
  6. if (len == || k % len == )
  7. {
  8. return;
  9. }
  10.  
  11. int mid = len - k % len;
  12.  
  13. reverseArray(nums, , mid - );
  14. reverseArray(nums, mid, len - );
  15. reverseArray(nums, , len - );
  16. }
  17.  
  18. void reverseArray(vector<int>& nums, int start, int end)
  19. {
  20. int s = start;
  21. int e = end;
  22.  
  23. while (s <= e)
  24. {
  25. int temp = nums[s];
  26. nums[s] = nums[e];
  27. nums[e] = temp;
  28. s++;
  29. e--;
  30. }
  31. }
  32. };

注意:

1、边界值检查,避免对0取余和长度不合法

2、先分别翻转,再总体翻转,注意下标

解法二:

  1. class Solution
  2. {
  3. public:
  4. void rotate(int nums[], int n, int k)
  5. {
  6. k = k%n;
  7.  
  8. // Reverse the first n - k numbers.
  9. // Index i (0 <= i < n - k) becomes n - k - i.
  10. reverse(nums, nums + n - k);
  11.  
  12. // Reverse tha last k numbers.
  13. // Index n - k + i (0 <= i < k) becomes n - i.
  14. reverse(nums + n - k, nums + n);
  15.  
  16. // Reverse all the numbers.
  17. // Index i (0 <= i < n - k) becomes n - (n - k - i) = i + k.
  18. // Index n - k + i (0 <= i < k) becomes n - (n - i) = i.
  19. reverse(nums, nums + n);
  20. }
  21. };

解法三:

  1. public class Solution {
  2. public void rotate(int[] nums, int k) {
  3. k %= nums.length;
  4. reverse(nums, , nums.length - );
  5. reverse(nums, , k - );
  6. reverse(nums, k, nums.length - );
  7. }
  8. public void reverse(int[] nums, int start, int end) {
  9. while (start < end) {
  10. int temp = nums[start];
  11. nums[start] = nums[end];
  12. nums[end] = temp;
  13. start++;
  14. end--;
  15. }
  16. }
  17. }

先整体搞,再分开搞

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. Hibernate 配置文件precision与scale

    Oracle使用标准.可变长度的内部格式来存储数字.这个内部格式精度可以高达38位. NUMBER数据类型可以有两个限定符,如: column NUMBER ( precision, scale) 表 ...

  2. 【线段树】bzoj3038 上帝造题的七分钟2 / bzoj3211 花神游历各国

    暴力修改,记录一段是否全部为1或0,若全是了,则不再修改. 注意3211一定要判是否为0,否则会T得惨无人道. #include<cstdio> #include<cmath> ...

  3. [TC-HouseProtection]House Protection

    题目大意: 一个平面直角坐标系中有给定的$n(n\le50)$个红点和$m(m\le50)$个蓝点,每个点可以选择画一个半径为$r$(所有的$r$相同)的圆或不画.圆的半径上限为$R(R\le1000 ...

  4. @requestBody注解的使用(上)

    1.@requestBody注解常用来处理content-type不是默认的application/x-www-form-urlcoded编码的内容,比如说:application/json或者是ap ...

  5. 13test02:阶乘

    //假设32位int型变量y是表示最大人数的x的阶乘,即y=x!,当x最大值取什么时,y取最大值 //,且乘法不溢出. #include<iostream> using namespace ...

  6. cookie的secure、httponly属性设置

    cookie的secure.httponly属性设置 转载自:http://www.cnblogs.com/alanzyy/archive/2011/10/14/2212484.html 一.属性说明 ...

  7. fidder模拟post提交到PHP遇到的问题

    http头必须带上Content-type: application/x-www-form-urlencoded  之后 ,php 才能接收到post数据 1. 用php://input可以很便捷的取 ...

  8. 在Code First中自动创建Entity模型

    之前我在博客文章中介绍过如何使用Code First来创建数据库,通过CodeFirst的方式,可以大幅的减少开发人员的工作量,比传统的先创建库再ORM的方式简单了不少.但是,很多时候,特别是一些MI ...

  9. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:3.安装Oracle RAC-3.2.安装 cvuqdisk 软件包

    3.2.安装 cvuqdisk 软件包 3.2.1. 准备Oracle  Grid安装包 上传Grid .Oracle 安装文件: sftp> put E:\Software\linux.x64 ...

  10. Flexbox兼容性语法汇总

    Flexbox版本 flexbox从第一次出现至今总共有三个语法版本,他们分别是: "display:box;"  —  2009年的老版本 "display:flexb ...