Skip to content

Commit bf25914

Browse files
Merge pull request algorithm001#335 from ma451152002/20190415_58_maguangjun_week01
maguangjun: add week02
2 parents f52e249 + 4c27c12 commit bf25914

3 files changed

Lines changed: 141 additions & 1 deletion

File tree

Week_01/id_58/NOTE.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
1-
# 学习笔记
1+
# 学习笔记
2+
本周的学习总结:
3+
1、学会了GitHub的pull-requet的使用。平时我们都是直接用git提交,然后merge到master,
4+
对于github的pull-request的提交形式不太熟悉,通过微信群咨询和同学的帮助,我学会了
5+
这项功能的使用。
6+
2、另外感触比较深的如何结合工作和学习任务,本周共布置了16道题,我完成了3道题目。
7+
虽然完成了,但是还是与之前的心里目标相差很远,希望在接下来的一周,能够每天按质
8+
按量的完成学习任务。
9+
3、本周主要学习了链表的功能使用
10+
83题:删除链表中的所有重复元素
11+
21题:合并两个排序的链表的链表
12+
24题:交换两个相邻的结点
13+
4、通过上面题目的训练,我对链表的题目有了新的认识,对于链表题目的解题方法就是要
14+
借助画图,拿出笔,简单的画出操作的关系,就可以很好的维护好各个结点关系,避免各种
15+
找不到后续结点错误。
16+
5、对于后续的数组、栈、递归、排序的题目,在后续的过程中也要快速完成。
17+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package algorithm.Week_02.id_58;
2+
3+
/**
4+
* 242. Valid Anagram
5+
*
6+
* Given two strings s and t , write a function to determine if t is an anagram of s.
7+
*
8+
* Example 1:
9+
*
10+
* Input: s = "anagram", t = "nagaram"
11+
* Output: true
12+
* Example 2:
13+
*
14+
* Input: s = "rat", t = "car"
15+
* Output: false
16+
* Note:
17+
* You may assume the string contains only lowercase alphabets.
18+
*
19+
* Follow up:
20+
* What if the inputs contain unicode characters? How would you adapt your solution to such case?
21+
*
22+
* 分析:
23+
* 所谓有效的字母异位词,就是字母类型和大小都一致,但是是不同的单词,这样的两个单词互为字母异位词
24+
* 分别用26个字母数组来存储两个字母串,最后对比两个数组即可,发现有不一致的则返回false,若同相同则返回true。
25+
* @auther: guangjun.ma
26+
* @date: 2019/4/27 16:16
27+
* @version: 1.0
28+
*/
29+
public class LeetCode_242_058 {
30+
public boolean isAnagram(String s, String t) {
31+
//1、这里用一个26位的字符串数字做为存储介质,转化公式为 ch - 'a'
32+
int[] sCounts = new int[26];
33+
int[] tCounts = new int[26];
34+
35+
//2、存储s字符串
36+
for(char ch : s.toCharArray()){
37+
sCounts[ch - 'a'] ++;
38+
}
39+
40+
//3、存储t字符串
41+
for(char ch : t.toCharArray()){
42+
tCounts[ch - 'a'] ++;
43+
}
44+
45+
//4、比较两个字符串
46+
for (int i = 0; i < 26 ; i++){
47+
if(sCounts[i] != tCounts[i]){
48+
return false;
49+
}
50+
}
51+
52+
return true;
53+
54+
}
55+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package algorithm.Week_02.id_58;
2+
3+
import java.util.ArrayList;
4+
import java.util.Comparator;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.PriorityQueue;
9+
10+
/**
11+
* 692. Top K Frequent Words
12+
*
13+
* Given a non-empty list of words, return the k most frequent elements.
14+
*
15+
* Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower alphabetical order comes first.
16+
*
17+
* Example 1:
18+
* Input: ["i", "love", "leetcode", "i", "love", "coding"], k = 2
19+
* Output: ["i", "love"]
20+
* Explanation: "i" and "love" are the two most frequent words.
21+
* Note that "i" comes before "love" due to a lower alphabetical order.
22+
* Example 2:
23+
* Input: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4
24+
* Output: ["the", "is", "sunny", "day"]
25+
* Explanation: "the", "is", "sunny" and "day" are the four most frequent words,
26+
* with the number of occurrence being 4, 3, 2 and 1 respectively.
27+
* Note:
28+
* You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
29+
* Input words contain only lowercase letters.
30+
* Follow up:
31+
* Try to solve it in O(n log k) time and O(n) extra space.
32+
*
33+
* 分析 : 这里是取最大的值,可以借助Java里的最大堆的数据结构 PriorityQueue
34+
* @auther: guangjun.ma
35+
* @date: 2019/4/27 17:07
36+
* @version: 1.0
37+
*/
38+
public class LeetCode_692_058 {
39+
public List<String> topKFrequent(String[] words, int k) {
40+
Map<String,Integer> map = new HashMap();
41+
42+
//把元素都放入到map中
43+
for(String s : words){
44+
map.put(s,map.getOrDefault(s,0) + 1);
45+
}
46+
47+
//优先队列 最大堆
48+
PriorityQueue<Map.Entry<String,Integer>> queue = new PriorityQueue<>(new Comparator<Map.Entry<String,Integer>>(){
49+
@Override
50+
public int compare(Map.Entry<String,Integer> o1,Map.Entry<String,Integer> o2){
51+
if(o1.getValue() .equals(o2.getValue()) ){
52+
return o1.getKey().compareTo(o2.getKey());
53+
}
54+
return o2.getValue() - o1.getValue();
55+
}
56+
});
57+
58+
//将map中的元素加入到队列中
59+
queue.addAll(map.entrySet());
60+
61+
//依次取出k个值
62+
List<String> res = new ArrayList<>(k);
63+
for(int i = 0; i <k ; i++){
64+
res.add(queue.poll().getKey());
65+
}
66+
return res;
67+
68+
}
69+
}

0 commit comments

Comments
 (0)