Leetcode 解题报告
347. 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.
思路:算法上没有什么新颖的地方,主要是数据结构和相应函数的使用。
先用Map 来存储数值和数值出现的次数,再对map进行排序,排序的时候按照value的值排序,最后输出后k个值。
需要注意的是排序的方法,这里用到了Collections.sort()函数的其中一种方式,就是自定义compare函数,来实现自己想要的函数。
public List<Integer> topKFrequent(int[] nums, int k) {
Map<Integer,Integer> m = new HashMap<Integer,Integer>();
List<Integer> res = new ArrayList<Integer>();
for (int i = 0; i < nums.length; i++) {
if (m.containsKey(nums[i])) {
m.put(nums[i], m.get(nums[i])+1);
} else {
m.put(nums[i], 1);
}
}
List<Map.Entry<Integer, Integer>> l = new ArrayList<Map.Entry<Integer, Integer>>(m.entrySet());
Collections.sort(l, new Comparator<Map.Entry<Integer,Integer>>() {
public int compare(Map.Entry<Integer, Integer> me1, Map.Entry<Integer, Integer> me2) {
return me1.getValue().compareTo(me2.getValue());
}
});
int size = m.size();
for (int i = 0; i < k; i++) {
res.add(l.get(size - 1 - i).getKey());
}
return res;
}
96. Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?
For example,
Given n = 3, there are a total of 5 unique BST's.
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
思路:考虑根节点的可能性,有n种,对于每一个数字i(1<=i<=n)为根节点时,所对应的BST的数量是 左子树的数量*右子树的数量。
左右子树的节点数量之和为i-1,并且分别是从0~i-1 和i-1~0对应变化。用dp[i]表示有i个节点的BST的数量,以j表示左子树节点数量,初始条件为dp[0] = 1,则有
for j: 0~i-1
dp[i] += dp[j]*dp[i-1-j];
public int numTrees(int n) {
int[] dp = new int[n+1];
dp[0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 0;j <= i-1; j++) {
dp[i] += dp[j] * dp[i - 1 - j];
}
}
return dp[n];
}
338. Counting Bits --20160518
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.
Example:
For num = 5 you should return [0,1,1,2,1,2].
Follow up:
- It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass?
- Space complexity should be O(n).
- Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.
Hint:
- You should make use of what you have produced already.
- Divide the numbers in ranges like [2-3], [4-7], [8-15] and so on. And try to generate new range from previous.
- Or does the odd/even status of the number help you in calculating the number of 1s?
思路:这是一个找规律的题,前后数字的1的个数是有增长的规律的。在稿纸上写出来就可以清晰地看到,这里就不赘述,直接上代码。
public class S338 {
public int[] countBits(int num) {
int[] count = new int[num+1];
int k = 0;
for (int i = 1; i <= num;) {
int temp = (int)Math.pow(2, k);
for (int j = 0; j < temp; j++) {
count[i] = count[i - temp] + 1;
i++;
if(i > num) {
break;
}
}
k++;
}
return count;
}
}
Leetcode 解题报告的更多相关文章
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- leetCode解题报告5道题(六)
题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest s ...
- LeetCode解题报告—— Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- LeetCode解题报告—— Search in Rotated Sorted Array & Search for a Range & Valid Sudoku
1. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated(轮流,循环) at so ...
- LeetCode解题报告—— 2 Keys Keyboard & Longest Palindromic Substring & ZigZag Conversion
1. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...
- LeetCode解题报告—— 1-bit and 2-bit Characters & 132 Pattern & 3Sum
1. 1-bit and 2-bit Characters We have two special characters. The first character can be represented ...
- LeetCode解题报告--2Sum, 3Sum, 4Sum, K Sum求和问题总结
前言: 这几天在做LeetCode 里面有2sum, 3sum(closest), 4sum等问题, 这类问题是典型的递归思路解题.该这类问题的关键在于,在进行求和求解前,要先排序Arrays.sor ...
- leetcode解题报告(13):K-diff Pairs in an Array
描述 Given an array of integers and an integer k, you need to find the number of unique k-diff pairs i ...
随机推荐
- Object-c学习之路(oc点语法)
最近想学习object-c了自己上网找了一些资料自学了一下:oc中的点语法是为了java.c等的程序员更好的上手而添加的功能. 主函数 // // main.m // OcTest1 // // Cr ...
- socket网络编程快速上手(二)——细节问题(4)
5.慢系统调用及EINTR 还记得前面readn和writen函数么?里面有个EINTR,现在就来谈谈这个,这个很重要. Linux世界有个叫信号的东西,感觉他就像一位隐士,很少遇到他,而他又无处不在 ...
- StringEscapeUtils.unescapeHtml的使用
在做代码高亮时,从数据库中取出代码如下(节选): <pre class="brush: java;"> 需要的应该是<pre class=\"brush ...
- ASP.NET WebAPI HTTPS
参照文档 http://southworks.com/blog/2014/06/16/enabling-ssl-client-certificates-in-asp-net-web-api/ 第一 ...
- Arduino 各种模块篇 RGB LED灯
示例代码: 类似与这样的led,共阴rgb led,通过调节不同的亮度,组合成不同的颜色. 示例代码: /* 作者:极客工坊 时间:2012年12月18日 IDE版本号:1.0.1 发布地址:www. ...
- squid和varnish的小结
squid和varnish的小结 http://blog.haohtml.com/ 上周初步接触linux下的这2个反向缓存软件,都实验了一下,貌似squid还是比较顺利的,varnish则碰 ...
- Hadoop1.0.4伪分布式安装
前言: 目前,学习hadoop的目的是想配合其它两个开源软件Hbase(一种NoSQL数据库)和Nutch(开源版的搜索引擎)来搭建一个知识问答系统,Nutch从指定网站爬取数据存储在Hbase数据库 ...
- 你不知道的Eclipse用法:使用Allocation tracker跟踪Android应用内存分配
Android Tools中的DDMS带有一个很不错的跟踪内存分配的工具Allocation tracker.通过Alloction tracker,不仅知道分配了哪类对象,还可以知道在哪个线程.哪个 ...
- Mutex 实现进程独占资源
背景:开发一套依赖硬件运行的软件,为避免硬件资源调用冲突,一个时间内只允许运行一个软件进程. 方法:使用Mutex判断该软件是否已启动,如果是,关闭即将启动的软件. 代码: static class ...
- Docker集群实验环境布署--swarm【2 搭建本地镜像仓库】
在10.40.100.148上 # docker run -d -p 5000:5000 --restart=always --name docker-registry.venic.com - ...