Skip to content

Commit 1e94a94

Browse files
authored
Merge pull request #2021 from chjung99/main
[chjung99] WEEK 01 solutions
2 parents 95c65d7 + f500b9d commit 1e94a94

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed

climbing-stairs/chjung99.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int climbStairs(int n) {
3+
int[] dp = new int[46];
4+
dp[0] = 1;
5+
dp[1] = 1;
6+
dp[2] = dp[1] + dp[0];
7+
dp[3] = dp[2] + dp[1];
8+
9+
for (int i = 4; i <= n; i++) {
10+
dp[i] = dp[i-1] + dp[i-2];
11+
}
12+
return dp[n];
13+
}
14+
}
15+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
13+
ListNode head = initNode(list1, list2);
14+
ListNode ptr = head;
15+
16+
while (true){
17+
if (list1 != null && list2 != null) {
18+
if (list1.val < list2.val) {
19+
ptr.val = list1.val;
20+
list1 = list1.next;
21+
} else {
22+
ptr.val = list2.val;
23+
list2 = list2.next;
24+
}
25+
}
26+
else if (list1 == null && list2 != null) {
27+
ptr.val = list2.val;
28+
list2 = list2.next;
29+
}
30+
else if (list1 != null && list2 == null) {
31+
ptr.val = list1.val;
32+
list1 = list1.next;
33+
}
34+
if (list1 == null && list2 == null) break;
35+
ptr.next = new ListNode();
36+
ptr = ptr.next;
37+
}
38+
return head;
39+
}
40+
41+
public ListNode initNode(ListNode list1, ListNode list2) {
42+
if (list1 == null && list2 == null) return null;
43+
return new ListNode();
44+
}
45+
}
46+

same-tree/chjung99.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.util.*;
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* public class TreeNode {
6+
* int val;
7+
* TreeNode left;
8+
* TreeNode right;
9+
* TreeNode() {}
10+
* TreeNode(int val) { this.val = val; }
11+
* TreeNode(int val, TreeNode left, TreeNode right) {
12+
* this.val = val;
13+
* this.left = left;
14+
* this.right = right;
15+
* }
16+
* }
17+
*/
18+
class Solution {
19+
final int NULL_MARKER = Integer.MIN_VALUE;
20+
public boolean isSameTree(TreeNode p, TreeNode q) {
21+
Queue<Integer> OrderP = new ArrayDeque<>();
22+
Queue<Integer> OrderQ = new ArrayDeque<>();
23+
trevasalTree(p, OrderP);
24+
trevasalTree(q, OrderQ);
25+
26+
if (OrderP.size() != OrderQ.size()) return false;
27+
28+
while (!OrderP.isEmpty() && !OrderQ.isEmpty()){
29+
Integer a = OrderP.poll();
30+
Integer b = OrderQ.poll();
31+
if (!a.equals(b)) return false;
32+
}
33+
34+
return OrderP.isEmpty() && OrderQ.isEmpty();
35+
}
36+
public void trevasalTree(TreeNode t, Queue<Integer> queue){
37+
// System.out.print(t.val+ " ");
38+
if (t == null){
39+
queue.add(NULL_MARKER);
40+
return;
41+
}
42+
queue.add(t.val);
43+
trevasalTree(t.left, queue);
44+
trevasalTree(t.right, queue);
45+
46+
}
47+
}
48+

two-sum/chjung99.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int[] twoSum(int[] nums, int target) {
5+
HashMap<Integer, List<Integer>> map = new HashMap();
6+
for (int i = 0; i < nums.length; i++) {
7+
if (!map.containsKey(nums[i])) {
8+
map.put(nums[i], new ArrayList<Integer>());
9+
}
10+
map.get(nums[i]).add(i);
11+
}
12+
13+
for (Map.Entry<Integer, List<Integer>> e: map.entrySet()) {
14+
int other = target - e.getKey();
15+
if (map.containsKey(other)) {
16+
if (e.getKey() == other && map.get(other).size() > 1) {
17+
return new int[]{e.getValue().get(0), e.getValue().get(1)};
18+
}
19+
return new int[]{e.getValue().get(0), map.get(other).get(0)};
20+
}
21+
}
22+
return new int[]{};
23+
}
24+
}
25+

valid-parentheses/chjung99.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public boolean isValid(String s) {
5+
ArrayDeque<Character> stack = new ArrayDeque<>();
6+
for (int i = 0; i < s.length(); i++){
7+
if (s.charAt(i) == '(' || s.charAt(i) == '{' || s.charAt(i) == '[' ){
8+
stack.push(s.charAt(i));
9+
continue;
10+
}
11+
12+
if (s.charAt(i) == ')' && !stack.isEmpty() && stack.peek() == '('){
13+
stack.pop();
14+
continue;
15+
}
16+
17+
if (s.charAt(i) == '}' && !stack.isEmpty() && stack.peek() == '{'){
18+
stack.pop();
19+
continue;
20+
}
21+
22+
if (s.charAt(i) == ']' && !stack.isEmpty() && stack.peek() == '['){
23+
stack.pop();
24+
continue;
25+
}
26+
return false;
27+
}
28+
return stack.isEmpty();
29+
30+
}
31+
}
32+

0 commit comments

Comments
 (0)