Skip to content

【069-Week2】学习总结 #332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
GradyWong opened this issue Apr 27, 2019 · 1 comment
Open

【069-Week2】学习总结 #332

GradyWong opened this issue Apr 27, 2019 · 1 comment

Comments

@GradyWong
Copy link
Contributor

GradyWong commented Apr 27, 2019

本周做了两道哈希表的题目:

  1. 有效的字母异位词

这道题,首先使用HashMap<Character, Integer>统计字母出现的次数,分别统计了两个字符串,然后用HashMap的equals进行比较,虽然通过了测试,但是时间复杂度和空间复杂度都比较高;

在重构的过程中,想到了老师讲过数组是特殊的哈希表,题干也限定了只有小写字母,所以用数组进行了重新实现,用数组下标进行字母的表示,数组值保存出现次数,统计s字符串,遍历字符串t中的字母进行比较,时间复杂度和空间复杂度都降低了很多,算法效率提升很明显。

  1. 前K个高频单词

这道题的算法比较容易想到,数据结构部分:使用哈希表进行词频统计,使用队列进行前K个高频单词的存储,但是写代码的部分遇到一些问题,需要去重写优先级队列的compare方法,处理相同次数的时候,按照字母顺序排序,还有一个部分,就是在入队列的时候也有相同问题,单词出现次数与队列头相同,并且字母顺序小于队列头,要替换队列头的元素。

@MrWang5200
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants