作者: 负雪明烛
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. 1 <= A.length <= 10000
  2. -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++)的更多相关文章

  1. [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 ...

  2. 【LeetCode】932. Beautiful Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 构造法 递归 相似题目 参考资料 日期 题目地址:h ...

  3. 【LeetCode】189. Rotate Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...

  4. 【LeetCode】525. Contiguous Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 累积和 日期 题目地址:https://leetco ...

  5. 【LeetCode】896. Monotonic Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. Leetcode 912. Sort an Array

    class Solution: def sortArray(self, nums: List[int]) -> List[int]: return sorted(nums)

  7. 【LeetCode】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

  8. 【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)

    [LeetCode]153. Find Minimum in Rotated Sorted Array 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode. ...

  9. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

随机推荐

  1. 《Redis设计与实现》知识点目录

    Redis设计与实现 第一部分 数据结构与对象 第二章 简单动态字符串 p8 简单动态字符串SDS 2.1 SDS的定义 p9 每个sds.h/sdshdr结构表示一个SDS值 2.2 SDS与C字符 ...

  2. k8s集群中部署Rook-Ceph高可用集群

    先决条件 为确保您有一个准备就绪的 Kubernetes 集群Rook,您可以按照这些说明进行操作. 为了配置 Ceph 存储集群,至少需要以下本地存储选项之一: 原始设备(无分区或格式化文件系统) ...

  3. 第三个基础框架 — springMVC — 更新完毕

    1.什么是springMVC? 还是老规矩,百度百科一下 这里面说了一堆废话,去官网瞄一下 官网网址:https://docs.spring.io/spring-framework/docs/curr ...

  4. Spark基础:(二)Spark RDD编程

    1.RDD基础 Spark中的RDD就是一个不可变的分布式对象集合.每个RDD都被分为多个分区,这些分区运行在分区的不同节点上. 用户可以通过两种方式创建RDD: (1)读取外部数据集====> ...

  5. 【leetcode】212. Word Search II

    Given an m x n board of characters and a list of strings words, return all words on the board. Each ...

  6. Java文件操作(求各专业第一名的学生)

    两个文件:info.txt 存放学生基本信息 学号 学院 专业 姓名 1001 计算机学院 软件工程 刘月 1002 生物工程 服装设计 孙丽 score.txt存放分数信息 学号 学科 成绩 100 ...

  7. mybatis-插件开发

    在Executor.StatementHandler.parameterHandler.resultSetHandler创建的时候都有一步这样的操作xxxHandler=interceptorChai ...

  8. Spring DM 2.0 环境配置 解决Log4j问题

    搭建 spring dm 2.0 环境出的问题 log4j 的问题解决办法是 一.引入SpringDM2.0的Bundle,最后完成如下图所示:注意:要引入slf4j.api.slf4j.log4j. ...

  9. 【Linux】【Basis】文件系统

    FHS:Filesystem Hierarchy Standard Web site: https://wiki.linuxfoundation.org/lsb/fhs http://www.path ...

  10. linux 加密安全之AWK

    密钥 密钥一般是一串字符串或数字,在加密或者解密时传递给加密或者解密算法,以使算法能够正确对明文加密或密文解密. 加密算法 已知的加密算法有对称和非对称加密,也就是说你想进行加解密操作的时候需要具备密 ...