| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
3 files changed
| Original file line number | Diff line number | Diff 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 | + } | ||
| Original file line number | Diff line number | Diff 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 | + } | ||
| Original file line number | Diff line number | Diff 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 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments