Skip to content

Commit b481b72

Browse files
authoredApr 28, 2025
Merge pull request #1358 from sm9171/ver4/week4
[sm9171] Week 4 solutions
2 parents 126607d + cdc4180 commit b481b72

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int findMin(int[] nums) {
3+
int min = Integer.MAX_VALUE;
4+
5+
for (int i = 0; i < nums.length; i++) {
6+
if (nums[i] < min) {
7+
min = nums[i];
8+
}
9+
}
10+
return min;
11+
}
12+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public int maxDepth(TreeNode root) {
3+
if (root == null) {
4+
return 0;
5+
}
6+
int leftDepth = maxDepth(root.left);
7+
int rightDepth = maxDepth(root.right);
8+
return Math.max(leftDepth, rightDepth) + 1;
9+
}
10+
}

‎merge-two-sorted-lists/sm9171.java

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
3+
ListNode dummy = new ListNode(-1);
4+
ListNode node = dummy;
5+
6+
while (list1 != null && list2 != null) {
7+
if (list1.val < list2.val) {
8+
node.next = list1;
9+
list1 = list1.next;
10+
} else {
11+
node.next = list2;
12+
list2 = list2.next;
13+
}
14+
node = node.next;
15+
}
16+
17+
node.next = list1 != null ? list1 : list2;
18+
return dummy.next;
19+
}
20+
}

‎word-search/sm9171.java

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
public boolean exist(char[][] board, String word) {
3+
if (board == null || board.length == 0 || word == null || word.length() == 0) {
4+
return false;
5+
}
6+
7+
int rows = board.length;
8+
int cols = board[0].length;
9+
boolean[][] visited = new boolean[rows][cols];
10+
11+
for (int i = 0; i < rows; i++) {
12+
for (int j = 0; j < cols; j++) {
13+
if (dfs(board, visited, i, j, word, 0)) {
14+
return true;
15+
}
16+
}
17+
}
18+
return false;
19+
}
20+
21+
private static boolean dfs(char[][] board, boolean[][] visited, int i, int j, String word, int index) {
22+
if (index == word.length()) {
23+
return true;
24+
}
25+
26+
if (i < 0 || i >= board.length || j < 0 || j >= board[0].length || visited[i][j] || board[i][j] != word.charAt(index)) {
27+
return false;
28+
}
29+
30+
31+
visited[i][j] = true;
32+
33+
boolean found = dfs(board, visited, i + 1, j, word, index + 1) ||
34+
dfs(board, visited, i - 1, j, word, index + 1) ||
35+
dfs(board, visited, i, j + 1, word, index + 1) ||
36+
dfs(board, visited, i, j - 1, word, index + 1);
37+
38+
visited[i][j] = false;
39+
40+
return found;
41+
}
42+
}

0 commit comments

Comments
 (0)
Please sign in to comment.