Leetcode中常用排序 之 O(n)时间复杂度排序
来源:互联网 发布:软件架构分析师 编辑:程序博客网 时间:2024/05/21 15:47
通过使用额外的空间,达到O(n)的时间复杂度的排序方法
直接上代码:
import java.util.HashMap;public class Ranking_O_n { public int[]ranking(int []nums){ int []ranked=new int[nums.length]; int times=0, min=0,max=0,index=0; HashMap<Integer,Integer>h=new HashMap<Integer,Integer>(); for(int n:nums){ if(n<min)min=n; if(n>max)max=n; if(h.containsKey(n)){ times=h.get(n); times++; h.put(n,times); } else h.put(n, 1); } for(int n=min;n<=max;n++){ if(h.containsKey(n)){ times=h.get(n); for(int i=0;i<times;i++){ ranked[index]=n; index++; } } } return ranked; } public static void main(String[]args){ Ranking_O_n c=new Ranking_O_n(); int[]nums={1,2,1,1,2,3,5,6,10,2,55,2,33,44,28,48,21}; for(int n:c.ranking(nums)){ System.out.print(n+" "); } }}
0 0
- Leetcode中常用排序 之 O(n)时间复杂度排序
- 时间复杂度为O(n)的排序
- 排序算法-o(n)时间复杂度
- 时间复杂度为O(n)的排序
- 时间复杂度O(n),空间复杂度O(1)的排序
- O(N)时间复杂度的排序算法-计数排序
- 1~n无序数组时间复杂度为O(n)排序
- 1~n无序数组时间复杂度为O(n)排序
- 如何对n个数进行排序,要求时间复杂度O(n),空间复杂度O(1)
- 如何对n个数进行排序,要求时间复杂度O(n),空间复杂度O(1)
- 如何对n个数进行排序,要求时间复杂度O(n),空间复杂度O(1)
- 如何对n个数进行排序,要求时间复杂度O(n),空间复杂度O(1)
- 如何对n个数进行排序,要求时间复杂度O(n),空间复杂度O(1)
- 如何对n个数进行排序,要求时间复杂度O(n),空间复杂度O(1)
- 如何对n个数进行排序,要求时间复杂度O(n),空间复杂度O(1)
- 如何对n个数进行排序,要求时间复杂度O(n),空间复杂度O(1)
- 位图排序时间复杂度可达O(n)
- 时间复杂度为O(n+t)的排序
- Centos下关于ssh、scp与rsync设置与应用
- Java反射学习总结(五)
- Bootstrap来实现tab页的切换
- HDU-2722-Here We Go(relians) Again
- Android逆向工程
- Leetcode中常用排序 之 O(n)时间复杂度排序
- java并发编程面试宝典
- JDBC小结_1
- 像素是什么,一个像素有多大,像素和分辨率的关系
- Java核心技术-servlet
- 创建型、结构型、行为型模式
- Netty系列-简单示例
- 回调函数
- cpu 平均负载