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

Commit219c0de

Browse files
committed
Added 4 solutions
1 parentd333b34 commit219c0de

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

‎Easy/Design HashMap.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
classMyHashMap {
2+
3+
/** Initialize your data structure here. */
4+
int[]arr;
5+
publicMyHashMap() {
6+
arr =newint[1000001];
7+
Arrays.fill(arr, -1);
8+
}
9+
10+
/** value will always be non-negative. */
11+
publicvoidput(intkey,intvalue) {
12+
arr[key] =value;
13+
}
14+
15+
/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
16+
publicintget(intkey) {
17+
returnarr[key];
18+
}
19+
20+
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
21+
publicvoidremove(intkey) {
22+
arr[key] = -1;
23+
}
24+
}
25+
26+
/**
27+
* Your MyHashMap object will be instantiated and called as such:
28+
* MyHashMap obj = new MyHashMap();
29+
* obj.put(key,value);
30+
* int param_2 = obj.get(key);
31+
* obj.remove(key);
32+
*/

‎Easy/Unique Morse Code Words.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
classSolution {
2+
publicstaticintuniqueMorseRepresentations(String[]words) {
3+
String[]codes = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
4+
Set<String>set =newHashSet<>();
5+
6+
for (Stringword :words) {
7+
StringBuildersb =newStringBuilder();
8+
9+
for (charc :word.toCharArray()) {
10+
sb.append(codes[c-'a']);
11+
}
12+
13+
set.add(sb.toString());
14+
}
15+
16+
returnset.size();
17+
}
18+
}

‎Medium/Summary Ranges.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
classSolution {
2+
publicList<String>summaryRanges(int[]nums) {
3+
List<String>ranges =newArrayList<>();
4+
5+
if (nums.length ==0) {
6+
returnranges;
7+
}
8+
9+
intcount =1;
10+
inti =0;
11+
StringBuildersb =newStringBuilder();
12+
sb.append(nums[0]);
13+
intn =nums.length-1;
14+
15+
while (i <n) {
16+
if (nums[i+1] -nums[i] ==1) {
17+
count++;
18+
}
19+
else {
20+
if (count >1) {
21+
sb.append("->").append(nums[i]);
22+
ranges.add(sb.toString());
23+
count =1;
24+
}
25+
else {
26+
ranges.add(sb.toString());
27+
}
28+
sb =newStringBuilder();
29+
sb.append(nums[i+1]);
30+
}
31+
32+
i++;
33+
}
34+
35+
if (count >1) {
36+
sb.append("->").append(nums[i]);
37+
ranges.add(sb.toString());
38+
}
39+
else {
40+
ranges.add(sb.toString());
41+
}
42+
43+
returnranges;
44+
}
45+
}

‎Medium/Top K Frequent Words.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
classSolution {
2+
publicList<String>topKFrequent(String[]words,intk) {
3+
4+
Map<String,Integer>map =newHashMap<>();
5+
6+
for (Stringword :words) {
7+
map.put(word,map.getOrDefault(word,0) +1);
8+
}
9+
10+
Queue<Map.Entry<String,Integer>>maxHeap =
11+
newPriorityQueue<>((o1,o2)->o1.getValue() ==o2.getValue() ?
12+
o1.getKey().compareTo(o2.getKey()) :
13+
o2.getValue()-o1.getValue());
14+
15+
for(Map.Entry<String,Integer>entry:map.entrySet()) {
16+
maxHeap.offer(entry);
17+
}
18+
19+
List<String>topKWords =newArrayList<>();
20+
21+
while(topKWords.size() <k){
22+
topKWords.add(maxHeap.poll().getKey());
23+
}
24+
25+
returntopKWords;
26+
}
27+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp