FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Merge pull request #425 from rononz/master · feixiangcode/algorithm@55b7265 · GitHub

Commit 55b7265

Browse files
Merge pull request algorithm001#425 from rononz/master
week2 first commit
2 parents bd98aa9 + bc8d22f commit 55b7265

3 files changed

Lines changed: 79 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public boolean isAnagram(String s, String t) {
3+
if (s == null || t == null) {
4+
return false;
5+
}
6+
int[] map = new int[26];
7+
for (int i = 0; i < s.length(); i++) {
8+
char c = s.charAt(i);
9+
int index = c - 'a';
10+
map[index]++;
11+
}
12+
for (int i = 0; i < t.length(); i++) {
13+
char c = t.charAt(i);
14+
int index = c - 'a';
15+
map[index]--;
16+
}
17+
for (int i : map) {
18+
if (i != 0) {
19+
return false;
20+
}
21+
}
22+
return true;
23+
}
24+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public int findSecondMinimumValue(TreeNode root) {
12+
if (root.left == null) {
13+
return -1;
14+
}
15+
int ret = -1;
16+
if (root.left.val == root.val) {
17+
ret = findSecondMinimumValue(root.left);
18+
} else {
19+
ret = root.left.val;
20+
}
21+
int retRight = -1;
22+
if (root.right.val == root.val) {
23+
retRight = findSecondMinimumValue(root.right);
24+
} else {
25+
retRight = root.right.val;
26+
}
27+
if (retRight > root.val
28+
&& (ret == -1 || retRight < ret)) {
29+
ret = retRight;
30+
}
31+
return ret;
32+
}
33+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import static java.util.stream.Collectors.toList;
2+
import static java.util.Collections.reverseOrder;
3+
4+
import java.util.Map;
5+
import java.util.List;
6+
import java.util.Comparator;
7+
8+
class Solution {
9+
public List<String> topKFrequent(String[] words, int k) {
10+
Map<String, Integer> map = new TreeMap<>();
11+
for (String word : words) {
12+
map.merge(word, 1, Integer::sum);
13+
}
14+
List<String> list = map.entrySet()
15+
.stream()
16+
.sorted(reverseOrder(Map.Entry.comparingByValue()))
17+
.map(Map.Entry<String, Integer>::getKey)
18+
.limit(k)
19+
.collect(toList());
20+
return list;
21+
}
22+
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL