Skip to content

Commit 3222879

Browse files
committed
Added 3 solutions
1 parent b01eb30 commit 3222879

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public void sortColors(int[] nums) {
3+
4+
int n0 = -1;
5+
int n1 = -1;
6+
int n2 = -1;
7+
8+
for (int i=0;i<nums.length;i++) {
9+
if (nums[i] == 0) {
10+
nums[++n2] = 2;
11+
nums[++n1] = 1;
12+
nums[++n0] = 0;
13+
}
14+
else if (nums[i] == 1) {
15+
nums[++n2] = 2;
16+
nums[++n1] = 1;
17+
}
18+
else {
19+
nums[++n2] = 2;
20+
}
21+
}
22+
}
23+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public List<Integer> topKFrequent(int[] nums, int k) {
3+
List<Integer>[] bucket = new List[nums.length+1];
4+
Map<Integer, Integer> frequencyMap = new HashMap<>();
5+
for (int n : nums) {
6+
frequencyMap.put(n, frequencyMap.getOrDefault(n, 0) + 1);
7+
}
8+
9+
for (int key : frequencyMap.keySet()) {
10+
int frequency = frequencyMap.get(key);
11+
12+
if (bucket[frequency] == null) {
13+
bucket[frequency] = new ArrayList<>();
14+
}
15+
16+
bucket[frequency].add(key);
17+
}
18+
19+
List<Integer> res = new ArrayList<>();
20+
21+
for (int pos = bucket.length - 1; pos >= 0 && res.size() < k; pos--) {
22+
if (bucket[pos] != null) {
23+
res.addAll(bucket[pos]);
24+
}
25+
}
26+
27+
return res;
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for binary tree with next pointer.
3+
* public class TreeLinkNode {
4+
* int val;
5+
* TreeLinkNode left, right, next;
6+
* TreeLinkNode(int x) { val = x; }
7+
* }
8+
*/
9+
public class Solution {
10+
public void connect(TreeLinkNode root) {
11+
12+
TreeLinkNode start=root;
13+
14+
while(start!=null){
15+
16+
TreeLinkNode cur=start;
17+
18+
while(cur!=null){
19+
if(cur.left!=null) cur.left.next=cur.right;
20+
if(cur.right!=null && cur.next!=null) cur.right.next=cur.next.left;
21+
22+
cur=cur.next;
23+
}
24+
25+
start=start.left;
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)