-
Notifications
You must be signed in to change notification settings - Fork 126
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
[mintheon] Week 4 #813
Merged
+131
−0
Merged
[mintheon] Week 4 #813
Changes from all commits
Commits
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 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,36 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* public class ListNode { | ||
* int val; | ||
* ListNode next; | ||
* ListNode() {} | ||
* ListNode(int val) { this.val = val; } | ||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; } | ||
* } | ||
*/ | ||
class Solution { | ||
/** | ||
시간복잡도: O(N) | ||
공간복잡도: O(1) | ||
*/ | ||
public ListNode mergeTwoLists(ListNode list1, ListNode list2) { | ||
ListNode answer = new ListNode(-1); | ||
ListNode node = answer; | ||
|
||
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 answer.next; | ||
} | ||
} |
This file contains 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,17 @@ | ||
class Solution { | ||
/** | ||
시간복잡도: O(n) | ||
공간복잡도: O(1) | ||
*/ | ||
public int missingNumber(int[] nums) { | ||
int n = nums.length; | ||
int expectedSum = n * (n + 1) / 2; | ||
|
||
int arraySum = 0; | ||
for(int i = 0; i < nums.length; i++) { | ||
arraySum += nums[i]; | ||
} | ||
|
||
return expectedSum - arraySum; | ||
} | ||
} |
This file contains 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,33 @@ | ||
class Solution { | ||
/** | ||
시간복잡도: O(n^2) | ||
공간복잡도: O(1) | ||
-> 모든 부분 문자열을 확인하는 방식 대신 좀 더 최적화한 방식으로 다시 풀어볼 것. | ||
*/ | ||
public int countSubstrings(String s) { | ||
int count = 0; | ||
|
||
for(int i = 0; i < s.length(); i++) { | ||
for(int j = i; j < s.length(); j++) { | ||
if(isPalindrom(s, i, j)) { | ||
count++; | ||
} | ||
} | ||
} | ||
|
||
return count; | ||
} | ||
|
||
private boolean isPalindrom(String text, int left, int right) { | ||
while(left < right) { | ||
if(text.charAt(left) != text.charAt(right)) { | ||
return false; | ||
} | ||
|
||
left++; | ||
right--; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains 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,45 @@ | ||
class Solution { | ||
// BFS로 변환해서 다시 풀어볼 것 | ||
char[][] board; | ||
boolean[][] visited; | ||
int[] moveX = {-1, 1, 0, 0}; | ||
int[] moveY = {0, 0, -1, 1}; | ||
int row, col; | ||
|
||
public boolean exist(char[][] board, String word) { | ||
this.board = board; | ||
this.row = board.length; | ||
this.col = board[0].length; | ||
this.visited = new boolean[row][col]; | ||
|
||
for(int y = 0; y < row; y++) { | ||
for(int x = 0; x < col; x++) { | ||
if(this.hasWord(y, x, word, 0)) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private boolean hasWord(int y, int x, String word, int index) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if(index >= word.length()) { | ||
return true; | ||
} | ||
|
||
if(x < 0 || x >= col || y < 0 || y >= row || visited[y][x] || board[y][x] != word.charAt(index)) { | ||
return false; | ||
} | ||
|
||
this.visited[y][x] = true; | ||
for(int i = 0; i < 4; i++) { | ||
if(this.hasWord(y + moveY[i], x + moveX[i], word, index + 1)) { | ||
return true; | ||
} | ||
} | ||
|
||
this.visited[y][x] = false; | ||
return false; | ||
} | ||
} |
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.
사실상 삼중 for statement인데, O(n^3)으로 볼 수도 있을까요? 의견 궁금합니다
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.
for 루프가 3개라고 해서 무조건 n^3는 아니지만, 해당 코드는 말씀하신대로 n^3의 경우로 보이네요!
결국 while내 호출이 될때마다 최대 O(n) 만큼의 작업이 추가 수행되는 부분을 놓쳤네요.