[Leetcode]189: Rotate Array

Link : https://leetcode.com/problems/rotate-array/

Problem : Given an array, rotate the array to the right by k steps, where k is non-negative.

給一個array,給一個非負數k,將array中的數組向右轉k次。白話點的意思就是,[1,2,3,4,5,6],k為3,將整組數字像有走三次,[4,5,6,1,2,3]。

圖來自leetcode

Thought :
1.應該有滿多種做法。
2. changed = [4,5,6]+[1,2,3]

time complexity = O(k) , space complexity = O(1)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store