<p>Given an integer array <code>nums</code>, rotate the array to the right by <code>k</code> steps, where <code>k</code> is non-negative.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4,5,6,7], k = 3
<strong>Output:</strong> [5,6,7,1,2,3,4]
<strong>Explanation:</strong>
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]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [-1,-100,3,99], k = 2
<strong>Output:</strong> [3,99,-1,-100]
<strong>Explanation:</strong> 
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>
	<li><code>0 &lt;= k &lt;= 10<sup>5</sup></code></li>
</ul>

<p>&nbsp;</p>
<p><strong>Follow up:</strong></p>

<ul>
	<li>Try to come up with as many solutions as you can. There are at least <strong>three</strong> different ways to solve this problem.</li>
	<li>Could you do it in-place with <code>O(1)</code> extra space?</li>
</ul>

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

        // sol2
        // vector<int> v1(nums.begin(), nums.end() - k);
        // vector<int> v2(nums.begin() + nums.size() - k, nums.end());
        // v2.insert(v2.end(), v1.begin(), v1.end());
        // nums = v2;

        // sol3
        int n = nums.size();
        vector<int> v2(nums.begin() + nums.size() - k, nums.end());
        nums.insert(nums.begin(), v2.begin(), v2.end());
        nums.resize(n);

        // sol4
        // int idx = 0, preserve = nums[0];
        // for (int i = 0; i < nums.size(); ++i) {
        //     int tmp = nums[(idx + k) % nums.size()];
        //     nums[(idx + k) % nums.size()] = preserve;
        //     idx = (idx + k) % nums.size();
        //     preserve = tmp;
        // }
    }
};
28600