https://leetcode.com/problems/top-k-frequent-elements/
Given a non-empty array of integers, return the k most frequent elements.
For example,
Given
[1,1,1,2,2,3]
and k = 2, return
[1,2]
.
Note:
-
- You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
- Your algorithm's time complexity must be better than O(n log n), where n is the array's size.
频率排序问题,先HASH到MAP里,然后按照频率可以用桶排序,也可以用优先序列。
STL里vector, map, priority_queue和pair都重新学习了一遍,非常方便。
还学习了不少C++11的东西,比如Lambda 表达式, auto,unordered_map,Range-based for Statement。可惜Dev-C++ 4.9.9.2不支持,所以又写了C++98版本。
- auto可以自动推断变量类型,如果不修改值的话,可以用const auto&。特别是对iterator很方便。
- unordered_map性能比map更好。
- Range-based for Statement更简洁,不需要写长串语句。可以对数组和STL sequence whose range is defined by a begin() and end()使用。Note that the keyword is preferred in the for-range-declaration portion of the statement.
- C++11还可以像数组一样初始化vector,赋值一串数字。
1 #include 2 #include 3 #include 4 #include
Priority queue
1 #include 2 #include 3 #include 4 #include
Bucket sort
Lambda
http://baike.baidu.com/link?url=ZK0qILx8cb_8HUX13JvVUYdnlJBOvAPJdWF83wD-tDgQQwIQYAVzkhytf_3f7oidHLPeTyXSswQUWVW51W42Ri4Pp8vnnkFHBCloSSTyS9xIT2pAV8a2zknUKK1UkjhDMzr4O7-qrD6J-lkJxET4u_#4
C++ 中的 Lambda 表达式
https://msdn.microsoft.com/zh-cn/library/dd293608.aspx
C++ STL编程轻松入门
http://tech.163.com/05/0613/10/1M4EA0US00091589.html
STL (模板库)
http://baike.baidu.com/link?url=IZpCuRuYLNA31OpA8x1h3urVpkYzp2K8oCssu_nkwqOqFdrMhL9drd9FnAg-Ru_D2690xbC0e3Q-W9QxF47gsUXTXdMmS5Vl_jNeFK0GXOa
Map(STL关联容器)_百度百科
http://baike.baidu.com/subview/95826/8050590.htm#viewPageContent
map 类
https://msdn.microsoft.com/zh-CN/library/s44w4h2s.aspx
vector(Java与C++语言中的对象)_百度百科
http://baike.baidu.com/item/vector/3330482
vector 类
https://msdn.microsoft.com/zh-cn/library/9xd04bzs.aspx
vector可不可以像数组那样初始化-CSDN论坛-CSDN.NET-中国最大的IT技术社区
http://bbs.csdn.net/topics/250063911
STL List_百度百科
http://baike.baidu.com/view/4255293.htm
make_pair - C++ Reference
http://www.cplusplus.com/reference/utility/make_pair/
pair - C++ Reference
http://www.cplusplus.com/reference/utility/pair/
pair Structure
https://msdn.microsoft.com/zh-cn/library/t9zb6cdt(v=vs.110).aspx
auto(C/C++语言存储类型)_百度百科
http://baike.baidu.com/link?url=U31tXzre2-JD9poBPGYKojek5DhtJGkafkk-YiG1-E_-v6ClnMUBIMe5JZaVvgKWp1IJUPr8MmXskPcngzigEFqhzaIdrlfDZC7cJ9vluCm
auto(C++)
https://msdn.microsoft.com/zh-cn/library/dd293667(v=vs.140).aspx
c++11_百度百科
http://baike.baidu.com/view/7021472.htm
Range-based for Statement (C++)
https://msdn.microsoft.com/en-us/library/jj203382.aspx
priority_queue 类
https://msdn.microsoft.com/zh-cn/library/4ef4dae9.aspx
priority_queue - C++ Reference
http://www.cplusplus.com/reference/queue/priority_queue/
unordered_map 类
https://msdn.microsoft.com/zh-cn/library/bb982522.aspx
unordered_map - C++ Reference
http://www.cplusplus.com/reference/unordered_map/unordered_map/