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. Encode and Decode Strings -- LeetCode

    Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...

  2. [HTML/CSS]uploadify自定义按钮样式

    概述 在项目中经常用到uploadify上传插件,但是FLASH按钮的外观往往跟我们网页的设计的主题色不太搭配.这时就需要对其样式进行修改. 样式文件是uploadify.css. 打开这个文件后,你 ...

  3. oracle 对应的JDBC驱动 版本

    Oracle版本 jdk版本 推荐jar包 备注 Oracle 8i JDK 1.1.x classes111.zip   Oracle 8i JDK 1.1.x classes12.zip   Or ...

  4. LLBLGen Pro ORM 生成器

    LLBLGen Pro  ORM 生成器:         http://www.llblgen.com/default.aspx 支持多种 框架,多种数据库.

  5. [SVN] svn在linux下的使用(svn命令行)ubuntu 删除 新增 添加 提交 状态查询 恢复

    转载自:http://www.cnblogs.com/xulb597/archive/2012/07/18/2597311.html 合并步骤:(1)先切换到分支:(2)svn merge trunk ...

  6. Linux Bash严重漏洞修复方法

    日前Linux官方内置Bash中新发现一个非常严重安全漏洞,黑客可以利用该Bash漏洞完全控制目标系统并发起攻击,为了避免Linux服务器受影响,就要尽快修补该漏洞了.(漏洞参考https://acc ...

  7. 一起來玩鳥 Starling Framework(8)BitmapFont

    所謂BitmapFont,就是事先將我們會用到的字型,會用到的字,輸出成一張圖片,類似Sprite sheet,以及一個xml格式的Data file,然後我們一次將這文字圖片轉成Texture,up ...

  8. 一起來玩鳥 Starling Framework(9)Particle

    最後,來看看Starling裡一個很炫的功能:Particle.Particle屬於extension,所以要另外下載檔案:Starling-Extension-Particle-System.下載之 ...

  9. HTML5 Canvas 绘制库存变化折线 计算出库存周转率

    <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type&quo ...

  10. node.js开发平台

    1.EDP:基于Node.JS与NPM的企业级开发平台 什么是EDP? EDP是一个基于Node.JS与NPM的企业级前端应用的开发平台.主要通过命令行的方式使用.EDP提供了前端应用开发时经常使用的 ...