diff --git a/longest-substring-without-repeating-characters/donghyeon95.java b/longest-substring-without-repeating-characters/donghyeon95.java new file mode 100644 index 000000000..5fd043eba --- /dev/null +++ b/longest-substring-without-repeating-characters/donghyeon95.java @@ -0,0 +1,37 @@ +import java.util.HashMap; + +class Solution { + public int lengthOfLongestSubstring(String s) { + // subString을 찾아야 한다. + // for 문을 반복하면서 내가 가지고 있는 문자열에 있는 문자라면 + // 그 문자열까지의 길이를 기록하고 꼬리를 짜르고 다시 반복문 + int result = 0; + HashMap 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); + } +} + + + diff --git a/number-of-islands/donghyeon95.java b/number-of-islands/donghyeon95.java new file mode 100644 index 000000000..e2e95eed7 --- /dev/null +++ b/number-of-islands/donghyeon95.java @@ -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[0].length || newY<0 || newY >= grid.length || visited[newY][newX] || grid[newY][newX]=='0') continue; + dfs(newY, newX, grid); + } + } +} + + diff --git a/reverse-linked-list/donghyeon95.java b/reverse-linked-list/donghyeon95.java new file mode 100644 index 000000000..08ef84089 --- /dev/null +++ b/reverse-linked-list/donghyeon95.java @@ -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); + nextNode.next = result; + result = nextNode; + head = head.next; + } + + return result; + } +} + + diff --git a/set-matrix-zeroes/donghyeon95.java b/set-matrix-zeroes/donghyeon95.java new file mode 100644 index 000000000..74bf5a745 --- /dev/null +++ b/set-matrix-zeroes/donghyeon95.java @@ -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; + } + } + } +} + + diff --git a/unique-paths/donghyeon95.java b/unique-paths/donghyeon95.java new file mode 100644 index 000000000..4d03a976b --- /dev/null +++ b/unique-paths/donghyeon95.java @@ -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