-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy path39.recover-rotated-sorted-array.cpp
More file actions
83 lines (76 loc) · 1.72 KB
/
39.recover-rotated-sorted-array.cpp
File metadata and controls
83 lines (76 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Tag: Array, Sort, Simulation
// Time: O(N)
// Space: O(1)
// Ref: -
// Note: Rotated
// Given a **rotated** sorted array, return it to sorted array in-place.(Ascending)
//
// **Example 1:**
//
// Input:
// ```
// array = [4,5,1,2,3]
// ```
// Output:
// ```
// [1,2,3,4,5]
// ```
// Explanation:
//
// Restore the rotated sorted array.
//
// **Example 2:**
//
// Input:
// ```
// array = [6,8,9,1,2]
// ```
// Output:
// ```
// [1,2,6,8,9]
// ```
// Explanation:
//
// Restore the rotated sorted array.
//
// What is rotated array?
//
// - For example, the orginal array is [1,2,3,4], The rotated array of it can be [1,2,3,4], [2,3,4,1], [3,4,1,2], [4,1,2,3]
class Solution {
public:
/**
* @param nums: An integer array
* @return: nothing
*/
void recoverRotatedSortedArray(vector<int> &nums) {
// write your code here
int low = findMin(nums);
if (low > 0) {
reverse(nums, 0, low - 1);
reverse(nums, low, nums.size() - 1);
reverse(nums, 0, nums.size() - 1);
}
}
void reverse(vector<int> &nums, int left, int right) {
while (left < right) {
swap(nums[left++], nums[right--]);
}
}
int findMin(vector<int> &nums) {
int left = 0;
int right = nums.size() - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] == nums[right]) {
right--;
continue;
}
if (nums[mid] < nums[right]) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
};