Skip to content

Commit abb6eb5

Browse files
authored
Merge pull request #2117 from chjung99/main
2 parents 0b222f1 + 4355718 commit abb6eb5

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed

contains-duplicate/chjung99.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* time: O(n)
3+
* space: O(n)
4+
*/
5+
6+
import java.util.*;
7+
8+
class Solution {
9+
public boolean containsDuplicate(int[] nums) {
10+
Set<Integer> hash = new HashSet<>();
11+
12+
for (int num: nums){
13+
if (hash.contains(num)) return true;
14+
hash.add(num);
15+
}
16+
17+
return false;
18+
}
19+
}
20+

invert-binary-tree/chjung99.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* time: O(n)
3+
* space: O(n)
4+
*/
5+
6+
import java.util.*;
7+
8+
/**
9+
* Definition for a binary tree node.
10+
* public class TreeNode {
11+
* int val;
12+
* TreeNode left;
13+
* TreeNode right;
14+
* TreeNode() {}
15+
* TreeNode(int val) { this.val = val; }
16+
* TreeNode(int val, TreeNode left, TreeNode right) {
17+
* this.val = val;
18+
* this.left = left;
19+
* this.right = right;
20+
* }
21+
* }
22+
*/
23+
class Solution {
24+
public TreeNode invertTree(TreeNode root) {
25+
breadthFirstSearch(root);
26+
return root;
27+
}
28+
public void breadthFirstSearch(TreeNode node){
29+
Deque<TreeNode> queue = new ArrayDeque<>();
30+
if (node != null){
31+
queue.add(node);
32+
}
33+
34+
while (!queue.isEmpty()){
35+
TreeNode cur = queue.remove();
36+
37+
TreeNode tmp = cur.left;
38+
cur.left = cur.right;
39+
cur.right = tmp;
40+
41+
if (cur.left != null){
42+
queue.add(cur.left);
43+
}
44+
if (cur.right != null){
45+
queue.add(cur.right);
46+
}
47+
}
48+
}
49+
}
50+

number-of-1-bits/chjung99.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* time: O(log n)
3+
* space: O(1)
4+
*/
5+
class Solution {
6+
public int hammingWeight(int n) {
7+
int cnt = 0;
8+
while (n != 0){
9+
if (n % 2 == 1) cnt ++;
10+
n = (int) (n/2);
11+
}
12+
return cnt;
13+
}
14+
}
15+

reverse-linked-list/chjung99.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* time: O(n)
3+
* space: O(1)
4+
*/
5+
6+
/**
7+
* Definition for singly-linked list.
8+
* public class ListNode {
9+
* int val;
10+
* ListNode next;
11+
* ListNode() {}
12+
* ListNode(int val) { this.val = val; }
13+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
14+
* }
15+
*/
16+
class Solution {
17+
ListNode tail;
18+
public ListNode reverseList(ListNode head) {
19+
reverseNode(head);
20+
return tail;
21+
}
22+
23+
public void reverseNode(ListNode node){
24+
if (node == null) return;
25+
if (node.next == null) {
26+
tail = node;
27+
return;
28+
}
29+
30+
ListNode nextNode = node.next;
31+
node.next = null;
32+
33+
reverseNode(nextNode);
34+
35+
nextNode.next = node;
36+
37+
}
38+
}
39+

valid-anagram/chjung99.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* time: O(n)
3+
* space: O(1)
4+
*/
5+
6+
class Solution {
7+
public boolean isAnagram(String s, String t) {
8+
int[] countS = new int[26];
9+
int slen = s.length();
10+
int tlen = t.length();
11+
12+
if (slen != tlen) return false;
13+
14+
for (int i = 0; i < slen; i++){
15+
countS[s.charAt(i)-'a'] += 1;
16+
countS[t.charAt(i)-'a'] -= 1;
17+
}
18+
for (int i = 0; i < 26; i++){
19+
if (countS[i] != 0) return false;
20+
}
21+
return true;
22+
}
23+
}
24+

0 commit comments

Comments
 (0)