-
-
Notifications
You must be signed in to change notification settings - Fork 195
[jinhyungrhee] WEEK 07 solutions #1456
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
5 commits
Select commit
Hold shift + click to select a range
3604a60
add solution of number-of-islands
jinhyungrhee 6316472
add solution of reverse-linked-list
jinhyungrhee c78c100
add solution of unique-paths
jinhyungrhee 9e27a56
add solution of set-matrix-zeroes
jinhyungrhee 8945532
add solution of longest-substring-without-repeating-characters
jinhyungrhee 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
45 changes: 45 additions & 0 deletions
45
longest-substring-without-repeating-characters/jinhyungrhee.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,45 @@ | ||
import java.util.*; | ||
class Solution { | ||
public int lengthOfLongestSubstring(String s) { | ||
|
||
if (s.length() == 0) return 0; | ||
if (s.length() == 1) return 1; | ||
|
||
// sliding window | ||
HashSet<Character> table = new HashSet<>(); | ||
|
||
int left = 0; | ||
int right = 1; | ||
int end = s.length() - 1; | ||
|
||
table.add(s.charAt(left)); | ||
|
||
int maxSize = 0; | ||
while (right <= end) { | ||
|
||
if (!table.contains(s.charAt(right))) { | ||
table.add(s.charAt(right)); | ||
} else { | ||
/** [중복된 문자면 슬라이딩 윈도우 이동] | ||
1. 중복 문자를 만나면 left를 한칸씩 증가 | ||
2. 중복 문자가 Hash 에서 사라질 때까지 왼쪽 값 제거 | ||
3. 왼쪽 중복문자가 제거되었을 때 right 이동 | ||
*/ | ||
while(table.contains(s.charAt(right))) { | ||
table.remove(s.charAt(left)); | ||
left++; | ||
} | ||
table.add(s.charAt(right)); | ||
} | ||
|
||
int tableSize = table.size(); | ||
if (tableSize > maxSize) maxSize = tableSize; | ||
|
||
right++; | ||
} | ||
|
||
return maxSize; | ||
} | ||
|
||
} | ||
|
||
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 @@ | ||
class Solution { | ||
|
||
public int numIslands(char[][] grid) { | ||
|
||
int[][] dir = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; | ||
boolean[][] visited = new boolean[grid.length][grid[0].length]; | ||
|
||
int count = 0; | ||
for (int i = 0; i < grid.length; i++) { | ||
for (int j = 0; j < grid[0].length; j++) { | ||
if ('1' == grid[i][j] && !visited[i][j]) { | ||
dfs(grid, visited, dir , i, j); | ||
count++; | ||
} | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
public void dfs(char[][] grid, boolean[][] visited, int[][] dir, int x, int y) { | ||
|
||
if (x >= 0 && x < grid.length && y >= 0 && y < grid[0].length | ||
&& !visited[x][y] && '1' == grid[x][y]) { | ||
|
||
visited[x][y] = true; | ||
for (int i = 0; i < 4; i++) { | ||
int dx = x + dir[i][0]; | ||
int dy = y + dir[i][1]; | ||
dfs(grid, visited, dir ,dx, dy); | ||
} | ||
} | ||
} | ||
} |
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 @@ | ||
class Solution { | ||
public ListNode reverseList(ListNode head) { | ||
|
||
ListNode prev = null; | ||
ListNode curr = head; | ||
|
||
while(curr != null) { | ||
ListNode next = curr.next; | ||
curr.next = prev; | ||
prev = curr; | ||
curr = next; | ||
} | ||
|
||
return prev; | ||
} | ||
} |
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,29 @@ | ||
class Solution { | ||
|
||
static int MARKER = 987654321; | ||
public void setZeroes(int[][] matrix) { | ||
|
||
int width = matrix.length; | ||
int height = matrix[0].length; | ||
|
||
for (int i = 0; i < width; i++) { | ||
for (int j = 0; j < height; j++) { | ||
if(matrix[i][j] == 0) { | ||
for (int x = 0; x < width; x++) { | ||
if (matrix[x][j] != 0) matrix[x][j] = MARKER; | ||
} | ||
for (int y = 0; y < height; y++) { | ||
if (matrix[i][y] != 0) matrix[i][y] = MARKER; | ||
} | ||
} | ||
} | ||
} | ||
|
||
for (int i = 0; i < width; i++) { | ||
for (int j = 0; j < height; j++) { | ||
if (matrix[i][j] == MARKER) matrix[i][j] = 0; | ||
} | ||
} | ||
|
||
} | ||
} |
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,60 @@ | ||
class Solution { | ||
int count; | ||
public int uniquePaths(int m, int n) { | ||
/** | ||
1. dfs + backtrack => Time Limit Exceeded | ||
*/ | ||
//boolean[][] visited = new boolean[m][n]; | ||
//dfs(visited, 0, 0, m, n); | ||
|
||
/** | ||
2. DP | ||
- 저장되는 값은 해당 위치까지 도달할 수 있는 '고유한 경로의 수' | ||
*/ | ||
int[][] dp = new int[m][n]; | ||
|
||
for (int i = 0; i < m; i++) { | ||
dp[i][0] = 1; | ||
} | ||
for (int j = 0; j < n; j++) { | ||
dp[0][j] = 1; | ||
} | ||
|
||
for (int i = 1; i < m; i++) { | ||
for (int j = 1; j < n; j++) { | ||
// 이전에 왔던 값들을 더함(위, 왼쪽) | ||
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; | ||
} | ||
} | ||
|
||
return dp[m-1][n-1]; | ||
} | ||
|
||
/** | ||
1. DFS + backtrack | ||
*/ | ||
/** | ||
public void dfs(boolean[][] visited, int x, int y, int m, int n) { | ||
|
||
if (x == m - 1 && y == n - 1) { | ||
count++; | ||
return; | ||
} | ||
|
||
|
||
visited[x][y] = true; | ||
|
||
// 오른쪽으로 이동 + backtrack | ||
if (y + 1 < n && !visited[x][y + 1]) { | ||
dfs(visited, x, y + 1, m , n); | ||
} | ||
|
||
// 아래쪽으로 이동 + backtrack | ||
if (x + 1 < m && !visited[x + 1][y]) { | ||
dfs(visited, x + 1, y, m, n); | ||
} | ||
|
||
visited[x][y] = false; | ||
} | ||
*/ | ||
} |
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.
슬라이딩 윈도우와 HashSet으로 효율적으로 푸신거같습니다!