From 21586c4ec3d4e11b904b29672212daa007d72891 Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Sat, 18 Jan 2025 14:45:48 +0900 Subject: [PATCH 1/7] reversed linked list --- reverse-linked-list/eunhwa99.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 reverse-linked-list/eunhwa99.java diff --git a/reverse-linked-list/eunhwa99.java b/reverse-linked-list/eunhwa99.java new file mode 100644 index 000000000..da6583fe3 --- /dev/null +++ b/reverse-linked-list/eunhwa99.java @@ -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; +} + From 48228a28a5ddb1bfc5dc46a915aaeb8974c97461 Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Sat, 18 Jan 2025 14:45:57 +0900 Subject: [PATCH 2/7] number of islands --- number-of-islands/eunhwa99.java | 66 +++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 number-of-islands/eunhwa99.java diff --git a/number-of-islands/eunhwa99.java b/number-of-islands/eunhwa99.java new file mode 100644 index 000000000..bbe849198 --- /dev/null +++ b/number-of-islands/eunhwa99.java @@ -0,0 +1,66 @@ +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 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; + } + } +} From a48162d7de2502e7cc70384a4a068e73650f51e6 Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Sat, 18 Jan 2025 14:46:11 +0900 Subject: [PATCH 3/7] longest substring without repeating characters --- .../eunhwa99.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 longest-substring-without-repeating-characters/eunhwa99.java diff --git a/longest-substring-without-repeating-characters/eunhwa99.java b/longest-substring-without-repeating-characters/eunhwa99.java new file mode 100644 index 000000000..d13dbf97d --- /dev/null +++ b/longest-substring-without-repeating-characters/eunhwa99.java @@ -0,0 +1,28 @@ +import java.util.HashMap; +import java.util.Map; + +class Solution { + // 시간 복잡도: O(N) + // 공간 복잡도: O(N) + public int lengthOfLongestSubstring(String s) { + Map 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; + } +} From f4443eb97c8a73e1150c2eadfb59452b89e7729b Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Sat, 18 Jan 2025 15:05:19 +0900 Subject: [PATCH 4/7] unique paths --- unique-paths/eunhwa99.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 unique-paths/eunhwa99.java diff --git a/unique-paths/eunhwa99.java b/unique-paths/eunhwa99.java new file mode 100644 index 000000000..f2d1a75f1 --- /dev/null +++ b/unique-paths/eunhwa99.java @@ -0,0 +1,22 @@ +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]; + } +} From 1a86a2ae43eee9f82a43598a3d4fc434fcfada12 Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Sat, 18 Jan 2025 16:00:53 +0900 Subject: [PATCH 5/7] set matrix zeroes --- set-matrix-zeroes/eunhwa99.java | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 set-matrix-zeroes/eunhwa99.java diff --git a/set-matrix-zeroes/eunhwa99.java b/set-matrix-zeroes/eunhwa99.java new file mode 100644 index 000000000..fc52f9277 --- /dev/null +++ b/set-matrix-zeroes/eunhwa99.java @@ -0,0 +1,40 @@ +import java.util.HashSet; +import java.util.Set; + +// 시간 복잡도: O(row x col) +// 공간 복잡도: O(row x col) +class Solution { + + public void setZeroes(int[][] matrix) { + int row = matrix.length; + int col = matrix[0].length; + + // 행과 열에 0이 있는지 체크할 배열 + Set rowZero = new HashSet<>(); + Set colZero = new HashSet<>(); + + 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; + } + } + } +} + From c98f3187a0559b6611808f89670bda3e0d3883b9 Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Sat, 18 Jan 2025 16:04:38 +0900 Subject: [PATCH 6/7] =?UTF-8?q?=EA=B3=B5=EA=B0=84=20=EB=B3=B5=EC=9E=A1?= =?UTF-8?q?=EB=8F=84=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- set-matrix-zeroes/eunhwa99.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/set-matrix-zeroes/eunhwa99.java b/set-matrix-zeroes/eunhwa99.java index fc52f9277..ee723c5e2 100644 --- a/set-matrix-zeroes/eunhwa99.java +++ b/set-matrix-zeroes/eunhwa99.java @@ -2,7 +2,7 @@ import java.util.Set; // 시간 복잡도: O(row x col) -// 공간 복잡도: O(row x col) +// 공간 복잡도: O(row + col) class Solution { public void setZeroes(int[][] matrix) { @@ -38,3 +38,4 @@ public void setZeroes(int[][] matrix) { } } + From 94040815b6319b171bea58fd455c8f83a162f8a7 Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Sat, 18 Jan 2025 16:11:07 +0900 Subject: [PATCH 7/7] =?UTF-8?q?=EB=9D=BC=EC=9D=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- longest-substring-without-repeating-characters/eunhwa99.java | 1 + number-of-islands/eunhwa99.java | 1 + unique-paths/eunhwa99.java | 1 + 3 files changed, 3 insertions(+) diff --git a/longest-substring-without-repeating-characters/eunhwa99.java b/longest-substring-without-repeating-characters/eunhwa99.java index d13dbf97d..ea718219e 100644 --- a/longest-substring-without-repeating-characters/eunhwa99.java +++ b/longest-substring-without-repeating-characters/eunhwa99.java @@ -26,3 +26,4 @@ public int lengthOfLongestSubstring(String s) { return maxLength; } } + diff --git a/number-of-islands/eunhwa99.java b/number-of-islands/eunhwa99.java index bbe849198..080dcc13c 100644 --- a/number-of-islands/eunhwa99.java +++ b/number-of-islands/eunhwa99.java @@ -64,3 +64,4 @@ static class Position { } } } + diff --git a/unique-paths/eunhwa99.java b/unique-paths/eunhwa99.java index f2d1a75f1..25e45c4d9 100644 --- a/unique-paths/eunhwa99.java +++ b/unique-paths/eunhwa99.java @@ -20,3 +20,4 @@ public int uniquePaths(int m, int n) { return paths[m - 1][n - 1]; } } +