LeetCode 189. Rotate Array (旋转数组)

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



题目标签:Array

  题目给了我们一个数组 和 k。 让我们 旋转数组 k 次。

  这里有一个很巧妙的方法:

    利用数组的length - k 把数组 分为两半;

    reverse 左边和右边的数组;

    reverse 总数组。

  举一个例子:

  1 2 3 4 5 6 7  如果k = 3 的话, 会变成 5 6 7 1 2 3 4

  1 2 3 4 5 6 7  middle = 7 - 3 = 4,分为左边 4个数字,右边 3个数字

  4 3 2 1 7 6 5  分别把左右reverse 一下

  5 6 7 1 2 3 4  把总数组reverse 一下就会得到答案

Java Solution:

Runtime beats 15.37%

完成日期:04/13/2017

关键词:Array

关键点:利用k把数组分两半;reverse左右两边数组;reverse总数组

 1 public class Solution
 2 {
 3     public void rotate(int[] nums, int k)
 4     {
 5         if(nums == null || nums.length == 0 || k % nums.length == 0)
 6             return;
 7
 8         int turns = k % nums.length;
 9         int middle = nums.length - turns;
10
11         reverse(nums, 0, middle-1); // reverse left part
12         reverse(nums, middle, nums.length-1); // reverse right part
13         reverse(nums, 0, nums.length-1); // reverse whole part
14     }
15
16     public void reverse(int[] arr, int s, int e)
17     {
18         while(s < e)
19         {
20             int temp = arr[s];
21             arr[s] = arr[e];
22             arr[e] = temp;
23
24             s++;
25             e--;
26         }
27     }
28 }

参考资料:

http://www.cnblogs.com/grandyang/p/4298711.html

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

时间: 2024-10-18 12:10:09

LeetCode 189. Rotate Array (旋转数组)的相关文章

[LeetCode]12. Rotate Array旋转数组

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 pro

leetCode 189. Rotate Array 数组

189. Rotate Array 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

[LeetCode]189.Rotate Array

题目 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

LeetCode 189 Rotate Array(旋转数组)

翻译 通过K步将一个有着n个元素的数组旋转到右侧. 例如, 给定n = 7和k = 3,数组[1,2,3,4,5,6,7]会被旋转成[5,6,7,1,2,3,4]. 批注: 尽你可能尝试多种解决方案,这里至少存在3种不同的方式去解决这个问题. 原文 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 rotate

leetcode——189 Rotate Array Total(数组的翻转)

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 pr

[LeetCode] Rotate Array 旋转数组

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 pro

rotate array 旋转数组

class Solution {public: void rotate(vector<int>& nums, int k) { int n=nums.size(); int i=0; /* //------------------- //解法一  会超时 //-------------------- k=k%n; while(i<k){ int temp=nums[n-1]; for(int j=n-1;j>0;j--){ nums[j]=nums[j-1]; } nums

Java for LeetCode 189 Rotate Array

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]. 解题思路: JAVA实现如下: public void rotate(int[] nums, int k) { k%=nums.length; k=nums.length-k; int[] nums2=ne

[LeetCode] 61. Rotate List 旋转链表

Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: Input: 1->2->3->4->5->NULL, k = 2 Output: 4->5->1->2->3->NULL Explanation: rotate 1 steps to the right: 5->1->2->3-&g