【LeetCode】912. Sort an Array 解题报告(C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/sort-an-array/
题目描述
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Note:
1 <= A.length <= 10000-50000 <= A[i] <= 50000
题目大意
对一个数组进行排序。
解题方法
库函数排序
最简单的方法使用C++内置的sort函数排序,本质是优化了的快排。
时间复杂度是O(N*log(N)),空间复杂度是O(1).
class Solution {
public:
vector<int> sortArray(vector<int>& nums) {
sort(nums.begin(), nums.end());
return nums;
}
};
桶排序
桶排序就是遍历所有元素,把元素的个数累加到对应的桶上,最后进行一次遍历把统计的数字放到结果中即可。
时间复杂度是O(N),空间复杂度是O(1)(元素的大小上下限已经固定).
C++代码如下:
class Solution {
public:
vector<int> sortArray(vector<int>& nums) {
vector<int> count(100010, 0);
for (int num : nums) {
count[num + 50000]++;
}
vector<int> res;
for (int i = 0; i < count.size(); i ++) {
while (count[i]-- != 0) {
res.push_back(i - 50000);
}
}
return res;
}
};
红黑树排序
C++的map使用了红黑树结构,也可以达到统计各个元素出现的次数,而且遍历是按照Key有序的。
时间复杂度是O(N*log(N)),空间复杂度是O(N).
class Solution {
public:
vector<int> sortArray(vector<int>& nums) {
map<int, int> m;
for (int num : nums) {
m[num]++;
}
vector<int> res;
auto it = m.begin();
while(it != m.end()) {
res.insert(res.end(), it->second, it->first);
it ++;
}
return res;
}
};
归并排序
merge sort是把数组的左右两半部分都排序,然后做一个merge two sorted array的操作。
我写的代码定义区间都是[start, end),即左开右闭,需要注意一下定义。下同。
时间复杂度是O(N*log(N)),空间复杂度是O(N).
class Solution {
public:
vector<int> sortArray(vector<int>& nums) {
return mergeSort(nums, 0, nums.size());
}
// sort nums[start, end)
vector<int> mergeSort(vector<int>& nums, int start, int end) {
if (start + 1 == end) return vector<int>(1, nums[start]);
int L = end - start;
vector<int> A = mergeSort(nums, start, start + L / 2);
vector<int> B = mergeSort(nums, start + L / 2, end);
return merge(A, B);
}
// merge two sorted array
vector<int> merge(vector<int>& A, vector<int>& B) {
int M = A.size(), N = B.size();
if (M == 0) return B;
if (N == 0) return A;
vector<int> res;
auto ita = A.begin();
auto itb = B.begin();
while (ita != A.end() && itb != B.end()) {
if (*ita < *itb) {
res.push_back(*ita);
++ita;
} else {
res.push_back(*itb);
++itb;
}
}
if (ita != A.end())
res.insert(res.end(), ita, A.end());
if (itb != B.end())
res.insert(res.end(), itb, B.end());
return res;
}
};
快速排序
快速排序的思想是,找出pivot的位置,使得其左边的元素都比pivot小,右边的元素都比pivot大。然后再对左右两部分进行排序。
最坏时间复杂度是O(N^2),平均时间复杂度是O(N*log(N)),空间复杂度是O(1).
class Solution {
public:
vector<int> sortArray(vector<int>& nums) {
quickSort(nums, 0, nums.size());
return nums;
}
// sort nums[start, end)
void quickSort(vector<int>& nums, int start, int end) {
if (end - start <= 1) return;
// nums[j] in right position
int j = partition(nums, start, end);
// sort nums[start, j)
quickSort(nums, start, j);
// sort nums[j + 1, end)
quickSort(nums, j + 1, end);
}
// nums[start, end) partition by nums[start]
int partition(vector<int>& nums, int start, int end) {
int pivot = nums[start];
int i = start, j = end;
while (true) {
while (++i < end && nums[i] < pivot);
while (--j > start + 1 && nums[j] > pivot);
if (i > j) break;
swap(nums[i], nums[j]);
}
swap(nums[start], nums[j]);
return j;
}
};
日期
2019 年 9 月 16 日 —— 秋高气爽
【LeetCode】912. Sort an Array 解题报告(C++)的更多相关文章
- [LeetCode] 912. Sort an Array 数组排序
Given an array of integers nums, sort the array in ascending order. Example 1: Input: [5,2,3,1] Outp ...
- 【LeetCode】932. Beautiful Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 构造法 递归 相似题目 参考资料 日期 题目地址:h ...
- 【LeetCode】189. Rotate Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...
- 【LeetCode】525. Contiguous Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 累积和 日期 题目地址:https://leetco ...
- 【LeetCode】896. Monotonic Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Leetcode 912. Sort an Array
class Solution: def sortArray(self, nums: List[int]) -> List[int]: return sorted(nums)
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- 【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)
[LeetCode]153. Find Minimum in Rotated Sorted Array 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode. ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
随机推荐
- expr判断文件名以固定格式结尾
#!/bin/bash if expr "$1" : ".*\.sh" &>/dev/null then echo "okok" ...
- 微信小程序调试bug-日程计划类
首先嘤嘤嘤一下,破bug,改了我一天,摔(′д` )-彡-彡 写的个微信小程序 逻辑如下,正常的功能是,我可以新建,修改,查询(按筛选条件),删除某个日程信息,后面贴个页面,我的bug出现就很搞笑了, ...
- (转载) Java多线程技术
多线程编程一直是学员们比较头痛和心虚的地方,因为线程执行顺序的不可预知性和调试时候的困难,让不少人在面对多线程的情况下选择了逃避,采用单线程的方式,其实只要我们对线程有了明确的认识,再加上java内置 ...
- vc控制台程序关闭事件时的正确处理方式
百度可以找到很多关于这个问题解决的方法 关键控制台API函数:SetConsoleCtrlHandler 在支持C++ 11以上的编译器中,你可以这么做. SetConsoleCtrlHandler( ...
- 05 Windows安装python3.6.4+pycharm环境
windows安装python3.6.4环境 使用微信扫码关注微信公众号,并回复:"Python工具包",免费获取下载链接! 一.卸载python环境 卸载以下软件: 二.安装py ...
- Flannel 启动报错
[root@ ~]#: kubectl logs -f kube-flannel-plcbl -n kube-system kube-flannel I0601 16:58:55.456862 1 m ...
- MySQL:事务常用语句
Mysql(版本是8)的事务隔离级别 默认是RR:REPEATABLE-READ:可重复读 查看 当前隔离级别 全局隔离级别 修改 -- 当前修改 -- 设置成可重复读 SET transactio ...
- 数据库之JDBC
1.简单认识一下JDBC 1).JDBC是什么? java database connection java数据库连接 作用:就是为了java连接mysql数据库嘛 要详细的,就面向百度编 ...
- CAN总线常见的两种编码格式(Intel/Motorola)
在汽车电子行业的开发或者测试中,我们经常会看到CAN总线信号的常见的两种编码格式:Intel格式与Motorola格式. 讲解这两种格式之前,我们先来了解一些大端模式和小端模式,会对后面理解这两种编码 ...
- Flink(八)【Flink的窗口机制】
目录 Flink的窗口机制 1.窗口概述 2.窗口分类 基于时间的窗口 滚动窗口(Tumbling Windows) 滑动窗口(Sliding Windows) 会话窗口(Session Window ...