-
-
Notifications
You must be signed in to change notification settings - Fork 195
[sm9171] Week 4 solutions #1358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Solution { | ||
public int findMin(int[] nums) { | ||
int min = Integer.MAX_VALUE; | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
if (nums[i] < min) { | ||
min = nums[i]; | ||
} | ||
} | ||
return min; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Solution { | ||
public int maxDepth(TreeNode root) { | ||
if (root == null) { | ||
return 0; | ||
} | ||
int leftDepth = maxDepth(root.left); | ||
int rightDepth = maxDepth(root.right); | ||
return Math.max(leftDepth, rightDepth) + 1; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Solution { | ||
public ListNode mergeTwoLists(ListNode list1, ListNode list2) { | ||
ListNode dummy = new ListNode(-1); | ||
ListNode node = dummy; | ||
|
||
while (list1 != null && list2 != null) { | ||
if (list1.val < list2.val) { | ||
node.next = list1; | ||
list1 = list1.next; | ||
} else { | ||
node.next = list2; | ||
list2 = list2.next; | ||
} | ||
node = node.next; | ||
} | ||
|
||
node.next = list1 != null ? list1 : list2; | ||
return dummy.next; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
class Solution { | ||
public boolean exist(char[][] board, String word) { | ||
if (board == null || board.length == 0 || word == null || word.length() == 0) { | ||
return false; | ||
} | ||
|
||
int rows = board.length; | ||
int cols = board[0].length; | ||
boolean[][] visited = new boolean[rows][cols]; | ||
|
||
for (int i = 0; i < rows; i++) { | ||
for (int j = 0; j < cols; j++) { | ||
if (dfs(board, visited, i, j, word, 0)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private static boolean dfs(char[][] board, boolean[][] visited, int i, int j, String word, int index) { | ||
if (index == word.length()) { | ||
return true; | ||
} | ||
|
||
if (i < 0 || i >= board.length || j < 0 || j >= board[0].length || visited[i][j] || board[i][j] != word.charAt(index)) { | ||
return false; | ||
} | ||
|
||
|
||
visited[i][j] = true; | ||
|
||
boolean found = dfs(board, visited, i + 1, j, word, index + 1) || | ||
dfs(board, visited, i - 1, j, word, index + 1) || | ||
dfs(board, visited, i, j + 1, word, index + 1) || | ||
dfs(board, visited, i, j - 1, word, index + 1); | ||
|
||
visited[i][j] = false; | ||
|
||
return found; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
안녕하세요, 작성하신 코드 잘 보았습니다! 더미 노드 사용해서 엣지 케이스까지 잘 구현하신 것 같아요. 그냥 개인적으로 궁금한 점이 있다면 자바에서
LinkedList
를 제공하고 있는 걸로 아는데 직접 구현하신 이유가 있는지 여쭤보고 싶습니다!