Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit848c9ab

Browse files
committed
Added 2 solutions
1 parent3fef036 commit848c9ab

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

‎Easy/Most Common Word.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
classSolution {
2+
publicstaticStringmostCommonWord(Stringparagraph,String[]banned) {
3+
paragraph =paragraph.toLowerCase();
4+
List<String>bannedWords =Arrays.asList(banned);
5+
6+
String[]words =paragraph.trim().split("\\s+");
7+
Map<String,Integer>map =newLinkedHashMap<>();
8+
intmaxValue =Integer.MIN_VALUE;
9+
10+
for (Stringword :words) {
11+
12+
if (!Character.isAlphabetic(word.charAt(word.length()-1))) {
13+
word =word.substring(0,word.length()-1);
14+
}
15+
16+
if (!(word.equals(" ") ||word.equals("")) &&bannedWords.indexOf(word) == -1) {
17+
map.put(word,map.getOrDefault(word,0) +1);
18+
maxValue =Math.max(maxValue,map.get(word));
19+
}
20+
}
21+
22+
for (Stringword :map.keySet()) {
23+
if (map.get(word) ==maxValue) {
24+
returnword;
25+
}
26+
}
27+
28+
return"";
29+
}
30+
}

‎Hard/Merge K Sorted Lists.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
classSolution {
10+
publicListNodemergeKLists(ListNode[]lists) {
11+
if (lists.length ==0) {
12+
returnnull;
13+
}
14+
15+
PriorityQueue<ListNode>queue =newPriorityQueue<>(lists.length,newComparator<ListNode>() {
16+
@Override
17+
publicintcompare(ListNodeo1,ListNodeo2) {
18+
returno1.val -o2.val;
19+
}
20+
});
21+
22+
for (inti =0;i <lists.length;i++) {
23+
if (lists[i] !=null) {
24+
queue.add(lists[i]);
25+
}
26+
}
27+
28+
ListNodehead =newListNode(0);
29+
ListNodep =head;
30+
31+
while (!queue.isEmpty()) {
32+
ListNodenode =queue.poll();
33+
p.next =node;
34+
if (node.next !=null) {
35+
queue.add(node.next);
36+
}
37+
38+
p =p.next;
39+
}
40+
41+
returnhead.next;
42+
}
43+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp