LeetCode] 189. Rotate Array 旋轉數組

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

Example 1:

Input: [1,2,3,4,5,6,7] and k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]

Example 2:

Input: [-1,-100,3,99] and k = 2
Output: [3,99,-1,-100]
Explanation:
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]

Note:

Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
Could you do it in-place with O(1) extra space?

Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.

新題搶先刷,這道題標爲 Easy,應該不是很難,我們先來看一種 O(n) 的空間複雜度的方法,我們複製一個和 nums 一樣的數組,然後利用映射關係 i -> (i+k)%n 來交換數字。代碼如下:

解法一:
複製代碼

class Solution {
public:
void rotate(vector& nums, int k) {
vector t = nums;
for (int i = 0; i < nums.size(); ++i) {
nums[(i + k) % nums.size()] = t[i];
}
}
};

複製代碼

由於提示中要求我們空間複雜度爲 O(1),所以我們不能用輔助數組,上面的思想還是可以使用的,但是寫法就複雜的多,而且需要用到很多的輔助變量,我們還是要將 nums[idx] 上的數字移動到 nums[(idx+k) % n] 上去,爲了防止數據覆蓋丟失,我們需要用額外的變量來保存,這裏用了 pre 和 cur,其中 cur 初始化爲了數組的第一個數字,然後當然需要變量 idx 標明當前在交換的位置,還需要一個變量 start,這個是爲了防止陷入死循環的,初始化爲0,一旦當 idx 變到了 strat 的位置,則 start 自增1,再賦值給 idx,這樣 idx 的位置也改變了,可以繼續進行交換了。舉個例子,假如 [1, 2, 3, 4], K=2 的話,那麼 idx=0,下一次變爲 idx = (idx+k) % n = 2,再下一次又變成了 idx = (idx+k) % n = 0,此時明顯 1 和 3 的位置還沒有處理過,所以當我們發現 idx 和 start 相等,則二者均自增1,那麼此時 idx=1,下一次變爲 idx = (idx+k) % n = 3,就可以交換完所有的數字了。

因爲長度爲n的數組只需要更新n次,所以我們用一個 for 循環來處理n次。首先 pre 更新爲 cur,然後計算新的 idx 的位置,然後將 nums[idx] 上的值先存到 cur 上,然後把 pre 賦值給 nums[idx],這相當於把上一輪的 nums[idx] 賦給了新的一輪,完成了數字的交換,然後 if 語句判斷是否會變到處理過的數字,參見上面一段的解釋,我們用題目中的例子1來展示下面這種算法的 nums 的變化過程:

1 2 3 4 5 6 7
1 2 3 1 5 6 7
1 2 3 1 5 6 4
1 2 7 1 5 6 4
1 2 7 1 5 3 4
1 6 7 1 5 3 4
1 6 7 1 2 3 4
5 6 7 1 2 3 4

解法二:
複製代碼

class Solution {
public:
void rotate(vector& nums, int k) {
if (nums.empty() || (k %= nums.size()) == 0) return;
int start = 0, idx = 0, pre = 0, cur = nums[0], n = nums.size();
for (int i = 0; i < n; ++i) {
pre = cur;
idx = (idx + k) % n;
cur = nums[idx];
nums[idx] = pre;
if (idx == start) {
idx = ++start;
cur = nums[idx];
}
}
}
};

複製代碼

根據熱心網友 waruzhi 的留言,這道題其實還有種類似翻轉字符的方法,思路是先把前 n-k 個數字翻轉一下,再把後k個數字翻轉一下,最後再把整個數組翻轉一下:

1 2 3 4 5 6 7
4 3 2 1 5 6 7
4 3 2 1 7 6 5
5 6 7 1 2 3 4

解法三:
複製代碼

class Solution {
public:
void rotate(vector& nums, int k) {
if (nums.empty() || (k %= nums.size()) == 0) return;
int n = nums.size();
reverse(nums.begin(), nums.begin() + n - k);
reverse(nums.begin() + n - k, nums.end());
reverse(nums.begin(), nums.end());
}
};

複製代碼

由於旋轉數組的操作也可以看做從數組的末尾取k個數組放入數組的開頭,所以我們用 STL 的 push_back 和 erase 可以很容易的實現這些操作。

解法四:
複製代碼

class Solution {
public:
void rotate(vector& nums, int k) {
if (nums.empty() || (k %= nums.size()) == 0) return;
int n = nums.size();
for (int i = 0; i < n - k; ++i) {
nums.push_back(nums[0]);
nums.erase(nums.begin());
}
}
};

複製代碼

下面這種方法其實還蠻獨特的,通過不停的交換某兩個數字的位置來實現旋轉,根據網上這個帖子的思路改寫而來,數組改變過程如下:

1 2 3 4 5 6 7
5 2 3 4 1 6 7
5 6 3 4 1 2 7
5 6 7 4 1 2 3
5 6 7 1 4 2 3
5 6 7 1 2 4 3
5 6 7 1 2 3 4

解法五:
複製代碼

class Solution {
public:
void rotate(vector& nums, int k) {
if (nums.empty()) return;
int n = nums.size(), start = 0;
while (n && (k %= n)) {
for (int i = 0; i < k; ++i) {
swap(nums[i + start], nums[n - k + i + start]);
}
n -= k;
start += k;
}
}
};

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章