# Leetcode 189. Rotate Array ###### tags: `Leetcode(C++)` 題目 : https://leetcode.com/problems/rotate-array/ 。 想法 : 開一個一樣大的陣列儲存rotate後的陣列,用空間換取時間。 時間複雜度 : O(n)。 程式碼 : ``` class Solution { public: void rotate(vector<int>& nums, int k) { vector<int> tmp(nums.size()); for(int i=0 ; i<nums.size() ; i++){ tmp[(i+k) % nums.size()]=nums[i]; } nums=tmp; return; } }; ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up