Skip to content

Commit 17646cf

Browse files
authored
Merge pull request #2310 from chjung99/main
[chjung99] WEEK 12 solutions
2 parents 5a357c2 + eb9795f commit 17646cf

3 files changed

Lines changed: 70 additions & 0 deletions

File tree

palindromic-substrings/chjung99.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
func countSubstrings(s string) int {
2+
ans := 0
3+
n := len(s)
4+
5+
for i := 0; i < n; i++ {
6+
for j := i+1; j <= n; j++ {
7+
if (isPalindrom(s[i:j])) {
8+
ans++
9+
}
10+
}
11+
}
12+
return ans
13+
}
14+
15+
func isPalindrom(s string) bool {
16+
var rev []byte
17+
n := len(s)
18+
for i := n-1; i >= 0; i-- {
19+
rev = append(rev, s[i])
20+
}
21+
return string(rev) == s
22+
}
23+
24+

sum-of-two-integers/chjung99.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func getSum(a int, b int) int {
2+
for b != 0 {
3+
sumWithoutCarry := a ^ b
4+
carry := (a & b) << 1
5+
6+
a = sumWithoutCarry
7+
b = carry
8+
}
9+
return a
10+
}
11+
12+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public int[] topKFrequent(int[] nums, int k) {
3+
Map<Integer, Integer> map = new HashMap<>();
4+
int[] answer = new int[k];
5+
int n = nums.length;
6+
List<Data> result = new ArrayList<>();
7+
8+
for (int i = 0; i < n; i++) {
9+
int key = nums[i];
10+
if (map.get(key) == null) {
11+
map.put(key, 1);
12+
} else {
13+
map.put(key, map.get(key) + 1);
14+
}
15+
}
16+
for (int key: map.keySet()) {
17+
result.add(new Data(key, map.get(key)));
18+
}
19+
result.sort((a, b) -> b.count() - a.count());
20+
21+
for (int i = 0; i < k; i++) {
22+
answer[i] = result.get(i).key();
23+
}
24+
25+
return answer;
26+
27+
}
28+
record Data(
29+
int key,
30+
int count
31+
){}
32+
}
33+
34+

0 commit comments

Comments
 (0)