【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/ ...
随机推荐
- linux下面升级 Python版本并修改yum属性信息
最近需要在linux下使用python,故需要升级一下python版本,上网查询了一下相关资料,更新了一下linux下面的python环境,记录如下: linux下面升级 Python版本并修改yum ...
- php5.6升级7
1. 检查当前安装的 PHP查看当前 PHP 版本 php -v查看当前 PHP 相关的安装包 yum list installed | grep php2. 更换 RPM 源#Centos 5.X: ...
- kubernetes部署haproxy、keepalived为kube-apiserver做集群
也可以用nginx.keepalived做负载均衡,看大家的需求. # yum -y install haproxy keepalived haproxy的配置文件(三台一样): cat > / ...
- 3.5 Rust Generic Types, Traits, and Lifetimes
Every programming language has tools for effectively handling the duplication of concepts. In Rust, ...
- 虚机扩大容量与vm减少所占容量
Linux的虚拟机碎片整理 sudo dd if=/dev/zero of=/free bs=1M sudo rm -f /free 镜像压缩 移动镜像 VBoxManage internalcomm ...
- redis入门到精通系列(四):Jedis--使用java操作redis详解
(一)前言 如果不把数据库和后端语言联系起来,就起不到数据库应该要起到的作用.Java语言通过JDBC操作mysql,用Jedis操作redis.当然了,java操作redis的方式不止jedis一种 ...
- 【Java 与数据库】How to Timeout JDBC Queries
How to Timeout JDBC Queries JDBC queries by default do not have any timeout, which means that a quer ...
- springmvc中文件跨服务器传输的方法
//1.首先在tomcat的新端口上重新开启一个tomcat服务器fileuploadserver服务器,并且在webapps下新建一个uploads文件夹 //2.在业务服务器上书写前端页面和后端的 ...
- 线程开启的第一种方法:通过创建Thread的子类的对象的方式
package cn.itcast.demo16.demo06.Thread;/** * @author newcityman * @date 2019/7/22 - 21:47 */public c ...
- TSN(Time-Sensitive Networking)协议导读
前言 上一个主题我们介绍了TSN的发展历史和协议族现状,它为建立统一的确定性网络传输基础设施提供了有力保障. TSN是一套协议标准,以保证确定性信息在标准以太网的不同场景下的顺利传输.TSN协议族本身 ...