【LeetCode】#189 Rotate Array(C#)

● 問題

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.


● 思考點

k是多少就向右旋轉多少次,後來找到資料可以用Array的Reverse方法,可以更加速理解(效能也比較好)。


● Solution

public class Solution {
    public void Rotate(int[] nums, int k) {
        k = k % nums.Length;
        Array.Reverse(nums);

        Array.Reverse(nums, 0, k);
        Array.Reverse(nums, k, nums.Length - k);
    }
}