Skip to content

Commit a2f3681

Browse files
committed
add 0500 and 0501 java version
1 parent 34ac757 commit a2f3681

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
public String[] findWords(String[] words) {
2+
if(words == null){
3+
return null;
4+
}
5+
ArrayList<String> list = new ArrayList<>();
6+
String[] keyboards = {"qwertyuiop","asdfghjkl","zxcvbnm"};
7+
for (int i = 0; i < words.length; i++) {
8+
String word = words[i].toLowerCase();
9+
for (int j = 0; j < keyboards.length; j++) {
10+
// 先用word首字符确定属于哪一行
11+
if(keyboards[j].indexOf(word.charAt(0))>-1){
12+
// 判断word字符串所有字符是否都属于同一行
13+
boolean match = match(keyboards[j], word,list);
14+
if(match){
15+
list.add(words[i]);
16+
}
17+
break;
18+
}
19+
}
20+
}
21+
return list.toArray(new String[list.size()]);
22+
}
23+
24+
private boolean match(String keyboard, String word, ArrayList<String> list) {
25+
for (int i = 1; i <word.length() ; i++) {
26+
if(keyboard.indexOf(word.charAt(i))<0){
27+
return false;
28+
}
29+
}
30+
return true;
31+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
int max=0;
2+
int cur=0;
3+
TreeNode preNode = null ;
4+
public int[] findMode(TreeNode root) {
5+
ArrayList<Integer> list = new ArrayList<>();
6+
findMode(root, list);
7+
int[] res = new int[list.size()];
8+
for (int i = 0;i<list.size();i++){
9+
res[i] = list.get(i);
10+
}
11+
return res;
12+
}
13+
14+
private void findMode(TreeNode root, ArrayList<Integer> list) {
15+
if(root == null){
16+
return;
17+
}
18+
findMode(root.left, list);
19+
if(preNode!=null && root.val == preNode.val ){
20+
cur++;
21+
} else {
22+
cur = 1;
23+
}
24+
if(max < cur ){
25+
max = cur;
26+
list.clear();
27+
list.add(root.val);
28+
}else if(max == cur) {
29+
list.add(root.val);
30+
}
31+
preNode = root;
32+
findMode(root.right, list);
33+
}

0 commit comments

Comments
 (0)