-
-
Notifications
You must be signed in to change notification settings - Fork 195
[eunhwa99] week 7 #916
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
[eunhwa99] week 7 #916
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
21586c4
reversed linked list
48228a2
number of islands
a48162d
longest substring without repeating characters
f4443eb
unique paths
1a86a2a
set matrix zeroes
c98f318
공간 복잡도 수정
fc2aaad
Merge branch 'DaleStudy:main' into main
eunhwa99 9404081
라인 추가
eece4a7
Merge remote-tracking branch 'origin/main'
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
29 changes: 29 additions & 0 deletions
29
longest-substring-without-repeating-characters/eunhwa99.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,29 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
class Solution { | ||
// 시간 복잡도: O(N) | ||
// 공간 복잡도: O(N) | ||
public int lengthOfLongestSubstring(String s) { | ||
Map<Character, Integer> position = new HashMap<>(); | ||
int start = 0; // substring의 시작점 | ||
int maxLength = 0; | ||
|
||
for (int idx = 0; idx < s.length(); idx++) { | ||
char currentChar = s.charAt(idx); | ||
|
||
// 같은 문자가 이미 map 에 있고, 그 문자가 현재 substring에 포함된 문자인지 확인 | ||
if (position.containsKey(currentChar) && position.get(currentChar) >= start) { | ||
start = position.get(currentChar) + 1; | ||
// 같은 문자가 포함되지 않게 substring의 시작을 옮긴다. | ||
} | ||
|
||
maxLength = Math.max(maxLength, idx - start + 1); | ||
|
||
position.put(currentChar, idx); | ||
} | ||
|
||
return maxLength; | ||
} | ||
} | ||
|
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,67 @@ | ||
import java.util.LinkedList; | ||
import java.util.Queue; | ||
|
||
// BFS 사용 | ||
// 시간 복잡도 : O(MxN) | ||
// 공간 복잡도: O(MxN) | ||
class Solution { | ||
|
||
int[][] dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; | ||
|
||
public int numIslands(char[][] grid) { | ||
int row = grid.length; | ||
int col = grid[0].length; | ||
|
||
int total = 0; | ||
for (int i = 0; i < row; i++) { | ||
for (int j = 0; j < col; j++) { | ||
if (grid[i][j] == '1') { | ||
total++; | ||
BFS(grid, i, j, row, col); | ||
System.out.println(grid[i][j]); | ||
} | ||
} | ||
} | ||
return total; | ||
} | ||
|
||
private void BFS(char[][] grid, int r, int c, int sizeR, int sizeC) { | ||
Queue<Position> queue = new LinkedList<>(); | ||
|
||
queue.add(new Position(r, c)); | ||
grid[r][c] = '0'; // '0'으로 변경 (방문 체크) | ||
|
||
while (!queue.isEmpty()) { | ||
Position current = queue.poll(); | ||
int curR = current.r; | ||
int curC = current.c; | ||
|
||
for (int i = 0; i < 4; i++) { | ||
int dirR = dir[i][0]; | ||
int dirC = dir[i][1]; | ||
|
||
int nextR = curR + dirR; | ||
int nextC = curC + dirC; | ||
|
||
if (nextR < 0 || nextR >= sizeR || nextC < 0 || nextC >= sizeC || grid[nextR][nextC] == '0') { | ||
continue; | ||
} | ||
queue.add(new Position(nextR, nextC)); | ||
grid[nextR][nextC] = '0'; | ||
} | ||
} | ||
|
||
} | ||
|
||
static class Position { | ||
|
||
int r; | ||
int c; | ||
|
||
Position(int r, int c) { | ||
this.r = r; | ||
this.c = c; | ||
} | ||
} | ||
} | ||
|
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 @@ | ||
/** | ||
* 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) { | ||
if(head==null) return head; | ||
|
||
ListNode pointer = new ListNode(head.val); | ||
|
||
ListNode tempPointer; | ||
while(head.next!=null){ | ||
tempPointer = new ListNode(head.next.val, pointer); | ||
pointer = tempPointer; | ||
head = head.next; | ||
} | ||
} | ||
return pointer; | ||
} | ||
|
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 @@ | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
// 시간 복잡도: O(row x col) | ||
// 공간 복잡도: O(row + col) | ||
class Solution { | ||
|
||
public void setZeroes(int[][] matrix) { | ||
int row = matrix.length; | ||
int col = matrix[0].length; | ||
|
||
// 행과 열에 0이 있는지 체크할 배열 | ||
Set<Integer> rowZero = new HashSet<>(); | ||
Set<Integer> colZero = new HashSet<>(); | ||
eunhwa99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for (int i = 0; i < row; i++) { | ||
for (int j = 0; j < col; j++) { | ||
if (matrix[i][j] == 0) { | ||
rowZero.add(i); | ||
colZero.add(j); | ||
} | ||
} | ||
} | ||
|
||
// 행을 0으로 설정 | ||
for (int r : rowZero) { | ||
for (int c = 0; c < col; c++) { | ||
matrix[r][c] = 0; | ||
} | ||
} | ||
|
||
// 열을 0으로 설정 | ||
for (int c : colZero) { | ||
for (int r = 0; r < row; r++) { | ||
matrix[r][c] = 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,23 @@ | ||
class Solution { | ||
|
||
public int uniquePaths(int m, int n) { | ||
int[][] paths = new int[m][n]; | ||
|
||
for (int i = 0; i < m; i++) { | ||
paths[i][0] = 1; //가장 왼쪽 줄은 항상 경로가 1개 | ||
} | ||
|
||
for (int i = 0; i < n; i++) { | ||
paths[0][i] = 1; // 가장 윗줄은 항상 경로가 1개 | ||
} | ||
for (int i = 1; i < m; i++) { | ||
for (int j = 1; j < n; j++) { | ||
|
||
paths[i][j] = paths[i - 1][j] + paths[i][j - 1]; | ||
} | ||
} | ||
|
||
return paths[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.
Uh oh!
There was an error while loading. Please reload this page.