Skip to content

Commit 219c0de

Browse files
committed
Added 4 solutions
1 parent d333b34 commit 219c0de

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+
class MyHashMap {
2+
3+
/** Initialize your data structure here. */
4+
int[] arr;
5+
public MyHashMap() {
6+
arr = new int[1000001];
7+
Arrays.fill(arr, -1);
8+
}
9+
10+
/** value will always be non-negative. */
11+
public void put(int key, int value) {
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+
public int get(int key) {
17+
return arr[key];
18+
}
19+
20+
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
21+
public void remove(int key) {
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+
class Solution {
2+
public static int uniqueMorseRepresentations(String[] words) {
3+
String[] codes = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
4+
Set<String> set = new HashSet<>();
5+
6+
for (String word : words) {
7+
StringBuilder sb = new StringBuilder();
8+
9+
for (char c : word.toCharArray()) {
10+
sb.append(codes[c-'a']);
11+
}
12+
13+
set.add(sb.toString());
14+
}
15+
16+
return set.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+
class Solution {
2+
public List<String> summaryRanges(int[] nums) {
3+
List<String> ranges = new ArrayList<>();
4+
5+
if (nums.length == 0) {
6+
return ranges;
7+
}
8+
9+
int count = 1;
10+
int i = 0;
11+
StringBuilder sb = new StringBuilder();
12+
sb.append(nums[0]);
13+
int n = 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 = new StringBuilder();
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+
return ranges;
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+
class Solution {
2+
public List<String> topKFrequent(String[] words, int k) {
3+
4+
Map<String, Integer> map = new HashMap<>();
5+
6+
for (String word : words) {
7+
map.put(word, map.getOrDefault(word, 0) + 1);
8+
}
9+
10+
Queue<Map.Entry<String,Integer>> maxHeap =
11+
new PriorityQueue<>((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 = new ArrayList<>();
20+
21+
while(topKWords.size() <k){
22+
topKWords.add(maxHeap.poll().getKey());
23+
}
24+
25+
return topKWords;
26+
}
27+
}

0 commit comments

Comments
 (0)