diff --git a/longest-substring-without-repeating-characters/eunhwa99.java b/longest-substring-without-repeating-characters/eunhwa99.java new file mode 100644 index 000000000..ea718219e --- /dev/null +++ b/longest-substring-without-repeating-characters/eunhwa99.java @@ -0,0 +1,29 @@ +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; + } +} + diff --git a/number-of-islands/eunhwa99.java b/number-of-islands/eunhwa99.java new file mode 100644 index 000000000..080dcc13c --- /dev/null +++ b/number-of-islands/eunhwa99.java @@ -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 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; + } + } +} + 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; +} + diff --git a/set-matrix-zeroes/eunhwa99.java b/set-matrix-zeroes/eunhwa99.java new file mode 100644 index 000000000..ee723c5e2 --- /dev/null +++ b/set-matrix-zeroes/eunhwa99.java @@ -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 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; + } + } + } +} + + diff --git a/unique-paths/eunhwa99.java b/unique-paths/eunhwa99.java new file mode 100644 index 000000000..25e45c4d9 --- /dev/null +++ b/unique-paths/eunhwa99.java @@ -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]; + } +} +