Rotate Array 解答

Question

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

Solution 1 Bubble Rotate

Similar with bubble sort, time complexity O(k * n), space cost O(1)

 1 public class Solution {
 2     public void rotate(int[] nums, int k) {
 3         int length = nums.length;
 4         if (k >= length)
 5             k = k % length;
 6         if (k == 0)
 7             return;
 8         for (int i = 0; i < k; i++) {
 9             for (int j = length - 1; j > 0; j--) {
10                 int tmp = nums[j];
11                 nums[j] = nums[j - 1];
12                 nums[j - 1] = tmp;
13             }
14         }
15     }
16 }

Solution 2 Traverse

Key to this solution is to make a copy of original array. Time complexity O(n), space cost O(n).

Solution 3 Reversal

Assuming we are given {1,2,3,4,5,6} and order 2. The basic idea is:

1. Divide the array two parts: 1,2,3,4 and 5, 6

2. Rotate first part: 4,3,2,1,5,6

3. Rotate second part: 4,3,2,1,6,5

4. Rotate the whole array: 5,6,1,2,3,4

Time complexity O(n), space cost O(n)

 1 public class Solution {
 2     public void rotate(int[] nums, int k) {
 3         int length = nums.length;
 4         if (k >= length)
 5             k = k % length;
 6         if (k == 0)
 7             return;
 8         reverse(nums, 0, length - k - 1);
 9         reverse(nums, length - k, length - 1);
10         reverse(nums, 0, length - 1);
11     }
12     private void reverse(int[] nums, int start, int end) {
13         if (start == end || nums.length == 1)
14             return;
15         while (start < end) {
16             int tmp = nums[start];
17             nums[start] = nums[end];
18             nums[end] = tmp;
19             start++;
20             end--;
21         }
22     }
23 }
时间: 2024-10-19 09:02:17

Rotate Array 解答的相关文章

[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

189. Rotate Array

1. 问题描述 189. Rotate ArrayRotate 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

LeetCode Rotate Array 翻转数组

题意:给定一个数组,将该数组的后k位移动到前n-k位之前.(本题在编程珠玑中第二章有讲) 思路: 方法一:将后K位用vector容器装起来,再移动前n-k位到后面,再将容器内k位插到前面. 1 class Solution { 2 public: 3 void rotate(int nums[], int n, int k) { 4 if( !k || !n || n==1 || k==n ) return; 5 k %= n; 6 vector<int> cha; 7 cha.reserve

【LeetCode】189. Rotate Array

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 s

151. Reverse Words in a String &amp;&amp; 61. Rotate List &amp;&amp; 189. Rotate Array

151. Reverse Words in a String Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". What constitutes a word?A sequence of non-space characters constitutes a word. Cou

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

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 differen

2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe

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]. 思路: 我的思路:新建一个数组存放旋转后的内容,但是怎么把原数组的内容存放在数组中,不清楚. leetcode/discuss思路: 思路一:新建数组,复制原

LeetCode:(Array-189) Rotate Array

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 s