LeetCode # 189 Rotate Array Pointer Array bivariate bis

Description


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?



Thinking


A Solution

A copy of the source array so that the cover numscan be retrieved in the elements after the old value of that position to what is the next update thereby numsuse the old values.

Algorithm only needs to nums.size()update the operation, so the time complexity O(n); a copy of the array, space complexityO(n)

It takes 16ms, ranking 80%

class Solution {
public:
    void rotate(vector<int>& nums, int k) {
        if (nums.empty()) return;

        k = k % nums.size();
        if (!k) return;

        vector<int> backup = nums;
        for (int i = 0; i < nums.size(); ++i) {
            int next_idx = abs(i + k) % (nums.size());
            nums[next_idx] = backup[i];
        }
    }
};



Solution two

If the interval is observed only kelement update operations, such as [1 2 3 4 5], k = 2words, look [1 3 5]update operation three locations, then at each update operation, the real data is used only the last value of the index element is covered, such as update 3, we used the value 1and index 0. Bivariate therefore, double the value of its old position of the index is maintained, so that a solution of the optimization.

Additional Note that, because the solution is to pick two out of the interval of kthe elements, then there may be another part of the missing elements, such as the passage of [2 4]is ignored, because k = 2when the pointer is always in [1 3 5]the three data circulation between. Thus, further addition of an index start position saved start_idx, when the pointer is equal start_idx, it indicates the current update data into local death cycle, the need to start_idxhave the current pointer increment, thereby disengaging the endless loop.

Each location will have an array of final algorithm is executed once the update operation, so in total to perform nums.size()update operations, the time complexity is O(n); spent five temporary variable to hold the information necessary algorithms, space complexityO(1)

It takes 16ms, ranking 80%

class Solution {
public:
    void rotate(vector<int>& nums, int k) {
        if (nums.empty()) return;

        k = k % nums.size();
        if (!k) return;

        int idx = 0;
        int start_idx = idx;  // mark starting point
        int prev = INT_MAX;
        int cur = nums[idx];
        int update_cnt = nums.size();  // only update size() times
        while (update_cnt > 0) {
            int next_idx = abs(idx + k) % (nums.size());
            prev = cur;
            cur = nums[next_idx];
            nums[next_idx] = prev;
            idx = next_idx;

            // avoid endless loop caused by even k
            if (idx == start_idx) {
                ++start_idx;
                idx = start_idx;
                cur = nums[idx];
            }

            --update_cnt;
        }
    }
};



reference


Guess you like

Origin www.cnblogs.com/Bw98blogs/p/12664207.html