-
Notifications
You must be signed in to change notification settings - Fork 2
/
q0912.hpp
60 lines (55 loc) · 1.5 KB
/
q0912.hpp
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
//
// q0912.hpp
// LeetCode
//
// Created by NowOrNever on 29/08/2020.
// Copyright © 2020 DoubleL. All rights reserved.
//
#ifndef q0912_hpp
#define q0912_hpp
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <numeric>
#include <stack>
#include <string>
using namespace std;
void question0912(){
class Solution {
private:
void swap(vector<int> &nums, int i, int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
int partition(vector<int> &nums, int low, int high){
int flag = nums[low];
int left = low + 1;
int right = high;
while (left < right) {
while (left < right && nums[left] <= flag) left++;
while (left < right && nums[right] > flag) right--;
if (left < right) swap(nums, left++, right--);
}
if (nums[left] > flag) left--;
swap(nums, low, left);
return left;
}
void selfSort(vector<int> &nums, int low, int high){
if (high <= low) return;
int mid = partition(nums, low, high);
selfSort(nums, low, mid - 1);
selfSort(nums, mid + 1, high);
}
public:
vector<int> sortArray(vector<int>& nums) {
selfSort(nums, 0, (int)nums.size() - 1);
return nums;
}
};
}
#endif /* q0912_hpp */