[Leetcode] 189. Rotate Array Rotation

Source: Internet
Author: User

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 [5,6,7,1,2,3,4] to.

Note:
Try to come up as many solutions as can, there is at least 3 different ways to solve this problem.

Hint:
Could 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.

Solution 1: Use an extra copy space. Time Complexity:o (n). Space complexity:o (N)

Solution 2: Flip the front n-k element, flip the remaining k elements, and finally flip all the elements. O (n). Space Complexity:o (1)

Java:

public void rotate (int[] nums, int k) {    k%= nums.length;    Reverse (nums, 0, nums.length-1);    Reverse (nums, 0, k-1);    Reverse (nums, K, nums.length-1);} 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--;}    }

Python:

Class Solution:    def rotate (self, nums, K):        k%= len (nums)        self.reverse (nums, 0, Len (nums))        Self.reverse (nums, 0, K)        Self.reverse (Nums, K, Len (nums))    def reverse (self, nums, start, end): While        start & Lt End:            Nums[start], nums[end-1] = nums[end-1], Nums[start]            start + = 1            End-= 1

C + +: Make a extra copy and then rotate. Time Complexity:o (n). Space complexity:o (n).

Class solution     {public    :        void rotate (int nums[], int n, int k)         {            if (n = = 0) | | (k <= 0))            {                return;            }                        Make a copy of Nums            vector<int> numscopy (n);            for (int i = 0; i < n; i++)            {                Numscopy[i] = nums[i];            }                        Rotate the elements.            for (int i = 0; i < n; i++)            {                nums[(i + k)%n] = Numscopy[i];}}    ;

C + +: Reverse The first n-k elements, the last K-elements, and then all the n elements.

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);        }    ;

Similar topics:

[Leetcode] 189. Rotate Array Rotation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.