-
-
Notifications
You must be signed in to change notification settings - Fork 245
[jaejeong1] WEEK 07 Solutions #482
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
3 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
55 changes: 55 additions & 0 deletions
55
longest-substring-without-repeating-characters/jaejeong1.java
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,55 @@ | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
class SolutionLongestSubstring { | ||
|
||
public int lengthOfLongestSubstring(String s) { | ||
// 반복되는 문자열 중 가장 긴 문자열의 길이를 반환해라 | ||
|
||
// s.length = 0 또는 1이면 return s.length() | ||
// lt = 0, rt = 1 | ||
// s[lt] != s[rt] 이면 SubString 여부를 검사하고 True면 rt++, count++ | ||
// s[lt] == s[rt] 이면 쌓인 카운트 정답에 적용하고 lt++, rt=lt+1, count 초기화 | ||
// rt가 끝에 도달하면 그때까지 쌓인 정답 반환 | ||
|
||
// TC: O(N*M), 전체 문자열 길이 N * 부분 문자열 길이 M | ||
// SC: O(M), 부분 문자열 생성 공간 | ||
|
||
if (s.length() <= 1) { | ||
return s.length(); | ||
} | ||
|
||
int lt = 0; | ||
int rt = lt + 1; | ||
int answer = 0; | ||
int count = 0; | ||
while (rt <= s.length()) { | ||
while (rt <= s.length() && isSubstring(s.substring(lt, rt))) { | ||
count++; | ||
rt++; | ||
} | ||
answer = Math.max(answer, count); | ||
|
||
lt++; | ||
rt = lt + 1; | ||
count = 0; | ||
} | ||
return answer; | ||
} | ||
|
||
// TC: O(M), 부분 문자열 str에 중복이 없는 경우 str의 길이 | ||
// SC: O(M), 부분 문자열 str의 중복이 없는 경우 str의 길이 | ||
private boolean isSubstring(String str) { | ||
Set<Character> set = new HashSet<>(); | ||
set.add(str.charAt(0)); // 첫번째 문자는 바로 add | ||
// 두번째 문자부터 중복 검사 대상 | ||
for (int i = 1; i < str.length(); i++) { | ||
// 중복 문자가 있거나, 공백이면 바로 false 리턴 | ||
if (!set.add(str.charAt(i)) || str.charAt(i) == ' ') { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
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,41 @@ | ||
class SolutionNumberOfIslands { | ||
char[][] sharedGrid; | ||
int[] dx = new int[]{0, 0, -1, 1}; | ||
int[] dy = new int[]{-1, 1, 0, 0}; | ||
|
||
public int numIslands(char[][] grid) { | ||
// 풀이 | ||
// 네 모서리가 물로 둘러쌓여있으면 아일랜드 | ||
// 아일랜드의 개수를 반환해라 | ||
// 땅인 경우 DFS 돌려서 순회하자 | ||
// 상하좌우 확인하면서 땅이면 물로 변경하면서 순회한다 | ||
// DFS 1회 당 answer += 1 | ||
// TC: O(N), N은 배열 원소 개수 | ||
// SC: O(N) | ||
jaejeong1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var answer = 0; | ||
|
||
sharedGrid = grid; | ||
for (int i=0; i<grid.length; i++) { | ||
for (int j=0; j<grid[0].length; j++) { | ||
if (sharedGrid[i][j] == '1') { | ||
dfs(i, j); | ||
answer++; | ||
} | ||
} | ||
} | ||
|
||
return answer; | ||
} | ||
|
||
private void dfs(int i, int j) { | ||
sharedGrid[i][j] = '0'; | ||
|
||
for (int k=0; k<4; k++) { | ||
var x = i+dx[k]; | ||
var y = j+dy[k]; | ||
if (x >= 0 && y >= 0 && x < sharedGrid.length && y < sharedGrid[0].length && sharedGrid[x][y] == '1') { | ||
dfs(x, y); | ||
} | ||
} | ||
} | ||
} |
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,33 @@ | ||
|
||
//Definition for singly-linked list. | ||
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 SolutionReverseLinkedList { | ||
public ListNode reverseList(ListNode head) { | ||
// 풀이: 링크드리스트 방향을 현재 기준으로 뒤집고, 노드를 다음으로 옮기며 반복한다 | ||
// next = curr.next | ||
// prev > curr | ||
// prev < curr | ||
// prev = curr | ||
// curr = next | ||
// TC: O(N), head 길이 N만큼 | ||
// SC: O(1), prev/curr 2개만 메모리 사용 | ||
|
||
ListNode prev = null; | ||
ListNode curr = head; | ||
while(curr != null) { | ||
ListNode next = curr.next; | ||
curr.next = prev; | ||
prev = curr; | ||
curr = next; | ||
} | ||
|
||
return prev; | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.