-
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
[donghyeon95] Week 7 #953
Merged
+182
−0
Merged
[donghyeon95] Week 7 #953
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fd4d875
feat: Reverse Linked List #223
df69057
feat: Longest Substring Without Repeating Characters #243
916cfe1
feat: Number of Islands #258
b354bd2
feat: Unique Paths #273
841a070
fix: insert newline
e30b45e
feat: Set Matrix Zeroes #283
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
37 changes: 37 additions & 0 deletions
37
longest-substring-without-repeating-characters/donghyeon95.java
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,37 @@ | ||
import java.util.HashMap; | ||
|
||
class Solution { | ||
public int lengthOfLongestSubstring(String s) { | ||
// subString을 찾아야 한다. | ||
// for 문을 반복하면서 내가 가지고 있는 문자열에 있는 문자라면 | ||
// 그 문자열까지의 길이를 기록하고 꼬리를 짜르고 다시 반복문 | ||
int result = 0; | ||
HashMap<String, Boolean> hm = new HashMap<>(); | ||
StringBuilder nowString = new StringBuilder(); | ||
|
||
for (String charater: s.split("")) { | ||
System.out.println(charater); | ||
if (hm.containsKey(charater)) { | ||
// nowString | ||
int index = nowString.indexOf(charater); | ||
nowString = new StringBuilder(nowString.substring(index+1) + charater); | ||
result = Math.max(nowString.length(), result); | ||
String removedString = nowString.substring(0, index); | ||
// String의 길이는 최대 27 * n | ||
for (String c: removedString.split("")) { | ||
hm.remove(c); | ||
} | ||
} | ||
else { | ||
hm.put(charater, true); | ||
nowString.append(charater); | ||
} | ||
} | ||
|
||
|
||
return Math.max(nowString.length(), result); | ||
} | ||
} | ||
|
||
|
||
|
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 @@ | ||
class Solution { | ||
|
||
int[][] moves = {{1,0}, {-1,0}, {0,1}, {0,-1}}; | ||
boolean[][] visited; | ||
public int numIslands(char[][] grid) { | ||
// 섬의 갯수 => 0혹은 경계선으로 둘러싸인 것의 갯수 | ||
// dfs로 탐색 | ||
int result = 0; | ||
visited = new boolean[grid.length][grid[0].length]; | ||
|
||
for (int i=0; i<grid.length; i++) { | ||
for (int j=0; j<grid[0].length; j++) { | ||
if (grid[i][j] == '1' && !visited[i][j]) { | ||
dfs(i, j, grid); | ||
result++; | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public void dfs(int y, int x, char[][] grid) { | ||
visited[y][x] = true; | ||
|
||
for (int[] move: moves) { | ||
int newX = x + move[0]; | ||
int newY = y + move[1]; | ||
|
||
if (newX<0 || newX >= grid[0].length || newY<0 || newY >= grid.length || visited[newY][newX] || grid[newY][newX]=='0') continue; | ||
dfs(newY, newX, grid); | ||
} | ||
} | ||
} | ||
|
||
|
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,30 @@ | ||
/** | ||
* 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; } | ||
* } | ||
*/ | ||
|
||
// 시간 복잡도 : O(n) | ||
// 공간 복잡도 : O(n) | ||
class Solution { | ||
public ListNode reverseList(ListNode head) { | ||
// 반복문을 돌면서 f(x+1)의 next를 f(x)로 지정 | ||
ListNode result = null; | ||
|
||
while(head != null) { | ||
ListNode nextNode = new ListNode(head.val); | ||
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.
|
||
nextNode.next = result; | ||
result = nextNode; | ||
head = head.next; | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
|
||
|
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,58 @@ | ||
class Solution { | ||
public void setZeroes(int[][] matrix) { | ||
int rows = matrix.length; | ||
int cols = matrix[0].length; | ||
|
||
boolean firstRowHasZero = false; | ||
boolean firstColHasZero = false; | ||
|
||
// 1. 첫 번째 행과 열에 0이 있는지 확인 | ||
for (int i = 0; i < rows; i++) { | ||
if (matrix[i][0] == 0) { | ||
firstColHasZero = true; | ||
break; | ||
} | ||
} | ||
for (int j = 0; j < cols; j++) { | ||
if (matrix[0][j] == 0) { | ||
firstRowHasZero = true; | ||
break; | ||
} | ||
} | ||
|
||
// 2. 나머지 행렬에서 0 찾기 (첫 번째 행과 열에 기록) | ||
for (int i = 1; i < rows; i++) { | ||
for (int j = 1; j < cols; j++) { | ||
if (matrix[i][j] == 0) { | ||
matrix[i][0] = 0; // 해당 행 표시 | ||
matrix[0][j] = 0; // 해당 열 표시 | ||
} | ||
} | ||
} | ||
|
||
// 3. 첫 번째 행과 열의 정보를 기반으로 행렬 수정 | ||
for (int i = 1; i < rows; i++) { | ||
for (int j = 1; j < cols; j++) { | ||
if (matrix[i][0] == 0 || matrix[0][j] == 0) { | ||
matrix[i][j] = 0; | ||
} | ||
} | ||
} | ||
|
||
// 4. 첫 번째 열 복구 | ||
if (firstColHasZero) { | ||
for (int i = 0; i < rows; i++) { | ||
matrix[i][0] = 0; | ||
} | ||
} | ||
|
||
// 5. 첫 번째 행 복구 | ||
if (firstRowHasZero) { | ||
for (int j = 0; j < cols; j++) { | ||
matrix[0][j] = 0; | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
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,21 @@ | ||
import java.util.Arrays; | ||
|
||
class Solution { | ||
public int uniquePaths(int m, int n) { | ||
// f(x,y) = f(x-1, y) + f(x, y-1) | ||
int[][] dp = new int[m][n]; | ||
Arrays.fill(dp[0], 1); | ||
for (int i=0; i<m; i++) { | ||
dp[i][0] = 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]; | ||
} | ||
} | ||
|
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.
오.. 이러면 테두리 경계값을 따로 확인하지 않아도 넘어갈 일이 없겠군요..!