forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
227f7d8
commit a4e6b74
Showing
5 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
longest-substring-without-repeating-characters/imsosleepy.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,23 @@ | ||
// ๊ฐ ๋ฌธ์๋ฅผ ํ๋ฒ์ฉ ์ฒ๋ฆฌํ๋๋ก ์ฌ๋ผ์ด๋ฉ ์๋์ฐ ๋ฐฉ์์ ์ฑํ | ||
// ์๊ฐ๋ณต์ก๋ : O(n) - ๊ฐ ๋ฌธ์๋ฅผ ํ ๋ฒ์ฉ ์ฒ๋ฆฌ | ||
class Solution { | ||
public int lengthOfLongestSubstring(String s) { | ||
int maxLength = 0; | ||
int startIdx = 0; | ||
String currentSubstring = ""; | ||
|
||
for (int i = 0; i < s.length(); i++) { | ||
int duplicateIdx = currentSubstring.indexOf(s.charAt(i)); | ||
|
||
// ์ค๋ณต ๋ฌธ์๊ฐ ๋ฐ๊ฒฌ๋๋ฉด ์๋์ฐ์ ์์ ์์น๋ฅผ ์กฐ์ | ||
if (duplicateIdx != -1) { | ||
startIdx = startIdx + duplicateIdx + 1; | ||
} | ||
|
||
currentSubstring = s.substring(startIdx, i + 1); | ||
maxLength = Math.max(maxLength, currentSubstring.length()); | ||
} | ||
|
||
return maxLength; | ||
} | ||
} |
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,46 @@ | ||
// DFS๋ฅผ ์ด์ฉํ ํ์ด๋ ์๋๋ฐ ๊ฐ์ธ์ ์ผ๋ก BFS๋ฅผ ๋ ์ ํธํจ | ||
class Solution { | ||
public int numIslands(char[][] grid) { | ||
if (grid == null || grid.length == 0) return 0; | ||
|
||
int numIslands = 0; | ||
int rows = grid.length, cols = grid[0].length; | ||
|
||
for (int row = 0; row < rows; row++) { | ||
for (int col = 0; col < cols; col++) { | ||
if (grid[row][col] == '1') { | ||
numIslands++; | ||
bfs(grid, row, col); | ||
} | ||
} | ||
} | ||
|
||
return numIslands; | ||
} | ||
|
||
private void bfs(char[][] grid, int startRow, int startCol) { | ||
int rows = grid.length; | ||
int cols = grid[0].length; | ||
|
||
Queue<int[]> queue = new LinkedList<>(); | ||
queue.offer(new int[]{startRow, startCol}); | ||
|
||
grid[startRow][startCol] = '0'; | ||
|
||
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; | ||
|
||
while (!queue.isEmpty()) { | ||
int[] cell = queue.poll(); | ||
int row = cell[0], col = cell[1]; | ||
|
||
for (int[] dir : directions) { | ||
int newRow = row + dir[0]; | ||
|
||
if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols && grid[newRow][newCol] == '1') { | ||
queue.offer(new int[]{newRow, newCol}); | ||
grid[newRow][newCol] = '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,17 @@ | ||
// ์ด์ ๋ ธ๋์ ํค๋์ ํ์ฌ๋ ธ๋ ํค๋๋ฅผ ํ๋์ฉ ์ด๋ํ๋ฉด์ ๊ตํํ๋ฉด ์์ฐ์ค๋ฝ๊ฒ ์ฐ๊ฒฐ๋ฆฌ์คํธ๊ฐ ๋ค์งํ๋ค. | ||
// ๊ฐ ๋ ธ๋๋ฅผ ํ๋ฒ์ฉ ๋ฐฉ๋ฌธํ๋ฏ๋ก O(N)์ ์๊ฐ๋ณต์ก๋๋ฅผ ๊ฐ๋๋ค. | ||
class Solution { | ||
public ListNode reverseList(ListNode head) { | ||
ListNode prev = null; | ||
ListNode current = head; | ||
|
||
while (current != null) { | ||
ListNode nextNode = current.next; | ||
current.next = prev; | ||
prev = current; | ||
current = nextNode; | ||
} | ||
|
||
return prev; | ||
} | ||
} |
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,57 @@ | ||
// ์ฃผ์ด์ง ์กฐ๊ฑด์ ๊ทธ๋๋ก ์๋ฎฌ๋ ์ด์ ์ผ๋ก ๊ตฌํ | ||
class Solution { | ||
public void setZeroes(int[][] matrix) { | ||
int rows = matrix.length; | ||
int cols = matrix[0].length; | ||
boolean firstRowZero = false; | ||
boolean firstColZero = false; | ||
|
||
// 1. ์ฒซ ํ๊ณผ ์ฒซ ์ด์ 0์ด ์๋์ง ํ์ธ | ||
for (int r = 0; r < rows; r++) { | ||
if (matrix[r][0] == 0) { | ||
firstColZero = true; | ||
break; | ||
} | ||
} | ||
|
||
for (int c = 0; c < cols; c++) { | ||
if (matrix[0][c] == 0) { | ||
firstRowZero = true; | ||
break; | ||
} | ||
} | ||
|
||
// 2. ๋๋จธ์ง ์ ์ ํ์ํ๋ฉฐ ์ฒซ ํ๊ณผ ์ฒซ ์ด์ 0 ๊ธฐ๋ก | ||
for (int r = 1; r < rows; r++) { | ||
for (int c = 1; c < cols; c++) { | ||
if (matrix[r][c] == 0) { | ||
matrix[r][0] = 0; | ||
matrix[0][c] = 0; | ||
} | ||
} | ||
} | ||
|
||
// 3. ์ฒซ ํ๊ณผ ์ฒซ ์ด ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก ๋๋จธ์ง ์ ์ 0์ผ๋ก ์ค์ | ||
for (int r = 1; r < rows; r++) { | ||
for (int c = 1; c < cols; c++) { | ||
if (matrix[r][0] == 0 || matrix[0][c] == 0) { | ||
matrix[r][c] = 0; | ||
} | ||
} | ||
} | ||
|
||
// 4. ์ฒซ ํ ์ฒ๋ฆฌ | ||
if (firstRowZero) { | ||
for (int c = 0; c < cols; c++) { | ||
matrix[0][c] = 0; | ||
} | ||
} | ||
|
||
// 5. ์ฒซ ์ด ์ฒ๋ฆฌ | ||
if (firstColZero) { | ||
for (int r = 0; r < rows; r++) { | ||
matrix[r][0] = 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,20 @@ | ||
// dp[i][j] = dp[i - 1][j] + dp[i][j - 1] ์ ํ์์ ์ธ์ ๋๋ฐ ์ด๊ธฐ ๋ฐ์ดํฐ ์ธํ ์ 1๋ก ๋ง์ถ๋ฉด ๋๋ ๊ฒ ๋๋ฌธ์ ์งํ์ด ์๋์์ | ||
// ๊ฒฐ๊ณผ์ ์ผ๋ก GPT์ ๋์์ ๋ฐ์ | ||
class Solution { | ||
public int uniquePaths(int m, int n) { | ||
|
||
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]; | ||
} | ||
|
||
} |