1. 程式人生 > >LintCode 39: Recover Rotated Sorted Array (三步反轉法)

LintCode 39: Recover Rotated Sorted Array (三步反轉法)

經典的三部反轉法。 Time complexity O(n). Space complexity O(1). 一個小優化是在找pivot的時候可以用binary search (見LintCode 62)。但總的時間複雜度還是O(n)。

class Solution {
public:
    /**
     * @param nums: An integer array
     * @return: nothing
     */
    void recoverRotatedSortedArray(vector<int> &nums) {
        int pos = 0;
        int arrayLen = nums.size();
        
        for (int i = 1; i < arrayLen; ++i) {
            if (nums[i] < nums[i - 1]) {
                pos = i;
                break;
            } 
        }
        
        if (pos == 0) return;
        
        reverse(nums, 0, pos - 1);
        reverse(nums, pos, arrayLen - 1);
        reverse(nums, 0, arrayLen - 1);
    }

private:
    void reverse(vector<int> &nums, int start, int end) {
        
        while (start < end) {
            int temp = nums[start];
            nums[start] = nums[end];
            nums[end] = temp;
            start++;
            end--;
        }
    }
};