912 Sort an Array 排序數組
Description:
Given an array of integers nums, sort the array in ascending order.
Example:
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 5 * 10^4
-5 * 10^4 <= nums[i] <= 5 * 10^4
題目描述:
給你一個整數數組 nums,請你將該數組升序排列。
示例 :
示例 1:
輸入:nums = [5,2,3,1]
輸出:[1,2,3,5]
示例 2:
輸入:nums = [5,1,1,2,0,0]
輸出:[0,0,1,1,2,5]
提示:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000
思路:
快速排序
遞歸地將比 pivot 小的都放在 pivot 左邊, 比 pivot 大的都放在 pivot 右邊
時間複雜度爲 O(nlgn), 空間複雜度爲 O(lgn), 空間爲遞歸棧的深度
代碼:
C++:
class Solution
{
public:
vector<int> sortArray(vector<int>& nums)
{
sort(nums.begin(), nums.end());
return nums;
}
};
Java:
class Solution {
public int[] sortArray(int[] nums) {
quicksort(nums, 0, nums.length - 1);
return nums;
}
private void quicksort(int[] nums, int left, int right) {
if (left >= right) return;
int pivot = partition(nums, left, right);
quicksort(nums, left, pivot - 1);
quicksort(nums, pivot + 1, right);
}
private int partition(int[] nums, int left, int right) {
int pivot = nums[left];
while (left < right) {
while (left < right && nums[right] >= pivot) --right;
nums[left] = nums[right];
while (left < right && nums[left] <= pivot) ++left;
nums[right] = nums[left];
}
nums[left] = pivot;
return left;
}
}
Python:
class TopVotedCandidate:
def __init__(self, persons: List[int], times: List[int]):
self.times, self.winner, d, max_value, cur_winner = times, [], defaultdict(int), 0, persons[0]
for person in persons:
d[person] += 1
if d[person] >= max_value:
cur_winner = person
max_value = d[person]
self.winner.append(cur_winner)
def q(self, t: int) -> int:
return self.winner[bisect_right(self.times, t) - 1]
# Your TopVotedCandidate object will be instantiated and called as such:
# obj = TopVotedCandidate(persons, times)
# param_1 = obj.q(t)