-
-
Notifications
You must be signed in to change notification settings - Fork 195
[forest000014] Week 04 #805
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 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bd7cc59
Merge Two Sorted Lists
forest000014 c955ca4
Missing Number
forest000014 300d1c1
Word Search
forest000014 848dc5d
Coin Change
forest000014 138e4c5
Palindromic Substrings
forest000014 336cce3
Palindromic Substrings - PR 피드백 반영
forest000014 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,37 @@ | ||
/** | ||
|
||
(n = list1.length + list2.length) | ||
시간 복잡도: O(n) | ||
- 최악의 경우 전체 노드 순회 | ||
공간 복잡도: O(n) | ||
- 최악의 경우 전체 노드만큼 새 노드 생성 | ||
|
||
*/ | ||
class Solution { | ||
public ListNode mergeTwoLists(ListNode list1, ListNode list2) { | ||
ListNode head = new ListNode(); | ||
ListNode curr = head; | ||
|
||
while (true) { | ||
if (list1 == null) { | ||
curr.next = list2; | ||
break; | ||
} else if (list2 == null) { | ||
curr.next = list1; | ||
break; | ||
} | ||
|
||
if (list1.val <= list2.val) { | ||
curr.next = new ListNode(list1.val); | ||
curr = curr.next; | ||
list1 = list1.next; | ||
} else { | ||
curr.next = new ListNode(list2.val); | ||
curr = curr.next; | ||
list2 = list2.next; | ||
} | ||
} | ||
|
||
return head.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,16 @@ | ||
/* | ||
시간 복잡도: O(n) | ||
공간 복잡도: O(1) | ||
|
||
1 ~ n의 합이 n * (n + 1) / 2 라는 점을 활용 | ||
*/ | ||
class Solution { | ||
public int missingNumber(int[] nums) { | ||
int n = nums.length; | ||
int ans = n * (n + 1) / 2; | ||
for (int i = 0; i < n; i++) { | ||
ans -= nums[i]; | ||
} | ||
return ans; | ||
} | ||
} |
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,84 @@ | ||
/* | ||
Time Complexity: O(m * n * 4^(word.length)) | ||
Space Complexity: O(m * n) | ||
*/ | ||
|
||
class Solution { | ||
boolean[][] visited; | ||
int m, n; | ||
int len; | ||
int[] dr = {-1, 0, 1, 0}; // clockwise traversal | ||
int[] dc = {0, 1, 0, -1}; | ||
char board2[][]; | ||
String word2; | ||
|
||
public boolean exist(char[][] board, String word) { | ||
int[] cnt = new int[52]; | ||
board2 = board; | ||
word2 = word; | ||
m = board.length; | ||
n = board[0].length; | ||
visited = new boolean[m][n]; | ||
len = word.length(); | ||
|
||
// 1. for pruning, count characters in board and word respectively | ||
for (int i = 0; i < m; i++) { | ||
for (int j = 0; j < n; j++) { | ||
cnt[charToInt(board[i][j])]++; | ||
} | ||
} | ||
for (int i = 0; i < len; i++) { | ||
int idx = charToInt(word.charAt(i)); | ||
if (--cnt[idx] < 0) { | ||
return false; | ||
} | ||
} | ||
|
||
// 2. DFS | ||
for (int i = 0; i < m; i++) { | ||
for (int j = 0; j < n; j++) { | ||
if (board2[i][j] != word.charAt(0)) { | ||
continue; | ||
} | ||
if (dfs(i, j, 1)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private boolean dfs(int row, int col, int idx) { | ||
if (idx == len) { // end of word | ||
return true; | ||
} | ||
visited[row][col] = true; | ||
|
||
for (int i = 0; i < 4; i++) { | ||
int nr = row + dr[i]; | ||
int nc = col + dc[i]; | ||
if (nr < 0 || nr >= m || nc < 0 || nc >= n) { // check boundary of the board | ||
continue; | ||
} | ||
if (visited[nr][nc]) { // check visited | ||
continue; | ||
} | ||
if (board2[nr][nc] == word2.charAt(idx)) { | ||
if (dfs(nr, nc, idx + 1)) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
visited[row][col] = false; | ||
return false; | ||
} | ||
|
||
private int charToInt(char ch) { | ||
if (ch <= 'Z') { | ||
return ch - 'A'; | ||
} else { | ||
return ch - 'a' + 26; | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
while(true)
대신에while(list1 != null && list2 != null)
을 조건문으로 사용하고 내부 if ~ else를 while문이 끝난 후에 사용하는 방법도 있을것 같습니다!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.
아하... 그렇게 하는 게 좀 더 논리가 명확하겠네요!
사실 처음부터 while(true)로 짰던 것은 아니고, 최대한 불필요한 조건문을 줄이고 줄이다 보니 지금 코드가 나오게 되었는데요,
코드를 다시 읽어보니 이 조건문이 언제 종료가 되는 것인지, 항상 종료가 되긴 하는 것인지가 한 눈에 직관적으로 이해되지는 않을 수 있겠네요
제안해 주신 아이디어대로 수정해보겠습니다 😄