diff --git a/linked-list-cycle/se6816.java b/linked-list-cycle/se6816.java new file mode 100644 index 0000000000..d8bf24e110 --- /dev/null +++ b/linked-list-cycle/se6816.java @@ -0,0 +1,40 @@ +/** + 길이가 최대 10000이므로 10001까지 경우의 수를 돌아 확인하는 방법 + */ +public class Solution { + public boolean hasCycle(ListNode head) { + int max = 10001; + int idx = 0; + boolean result = false; + while(head != null) { + idx++; + head = head.next; + if(idx > max) { + result = true; + break; + } + } + + return result; + } +} +/** + 2개의 포인터를 이용하여, 만나는지 확인하는 방법 + */ +public class Solution2 { + public boolean hasCycle(ListNode head) { + ListNode first = head; + ListNode second = head; + boolean result = false; + while(second != null && second.next != null) { + first = first.next; + second = second.next.next; + if(first == second) { + result = true; + break; + } + } + + return result; + } +} diff --git a/number-of-islands/se6816.java b/number-of-islands/se6816.java new file mode 100644 index 0000000000..6c06456dd9 --- /dev/null +++ b/number-of-islands/se6816.java @@ -0,0 +1,52 @@ +/** + 방문하지 않는 지점을 찾고, BFS로 인근 지점을 방문처리하며, 방문하지 않는 지점 개수를 카운틷하는 방식 + */ +class Solution { + int M; + int N; + boolean[][] visited; + int[] moveX = {-1, 0, 0, 1}; + int[] moveY = {0, -1, 1, 0}; + public int numIslands(char[][] grid) { + int result = 0; + M = grid.length; + N = grid[0].length; + visited= new boolean[M][N]; + for(int i = 0; i < M; i++) { + for(int j = 0; j < N; j++) { + if(!visited[i][j] && grid[i][j] == '1') { + visited[i][j] = true; + result++; + paint(i, j, visited, grid); + } + } + } + return result; + + } + public void paint(int x, int y, boolean[][] visited, char[][] grid) { + for(int i = 0; i < 4; i++) { + int tempX = x + moveX[i]; + int tempY = y + moveY[i]; + + if(isOutOfIndex(tempX, tempY)) { + continue; + } + + if(visited[tempX][tempY]) { + continue; + } + + if(grid[tempX][tempY] == '0') { + continue; + } + + visited[tempX][tempY] = true; + + paint(tempX,tempY, visited, grid); + } + } + public boolean isOutOfIndex(int x, int y) { + return x < 0 || x >= M || y < 0 || y >=N; + } +} diff --git a/pacific-atlantic-water-flow/se6816.java b/pacific-atlantic-water-flow/se6816.java new file mode 100644 index 0000000000..a0c482a9b9 --- /dev/null +++ b/pacific-atlantic-water-flow/se6816.java @@ -0,0 +1,73 @@ +/** + BFS를 통해, 방문 지점이 겹치는 부분을 찾는 방식 + */ +class Solution { + public static int[] moveX = {0, 1, 0, -1}; + public static int[] moveY = {1, 0, -1, 0}; + public boolean[][] POvisited; + public boolean[][] AOvisited; + public int N; + public int M; + public List> pacificAtlantic(int[][] heights) { + List> result = new ArrayList<>(); + N = heights.length; + M = heights[0].length; + POvisited = new boolean[N][M]; + AOvisited = new boolean[N][M]; + Queue que = new LinkedList<>(); + Queue que2 = new LinkedList<>(); + + for(int i = 0; i < M; i++) { + POvisited[0][i] = true; + que.add(new int[]{0, i}); + AOvisited[N-1][i] = true; + que2.add(new int[]{N-1, i}); + } + + for(int i = 0; i < N; i++) { + POvisited[i][0] = true; + que.add(new int[]{i, 0}); + AOvisited[i][M-1] = true; + que2.add(new int[]{i, M-1}); + } + bfs(que, POvisited, heights); + bfs(que2, AOvisited, heights); + + for(int i = 0; i < N; i++) { + for(int j = 0; j < M; j++) { + if(POvisited[i][j] && AOvisited[i][j]) { + result.add(new ArrayList<>(List.of(i, j))); + } + } + } + + return result; + } + + public void bfs(Queue que, boolean[][] visited, int[][] heights) { + while(!que.isEmpty()) { + int[] node = que.poll(); + for(int i = 0; i < 4; i++) { + int tempX = node[0] + moveX[i]; + int tempY = node[1] + moveY[i]; + if(isOutOfIndex(tempX, tempY)) { + continue; + } + + if(visited[tempX][tempY]) { + continue; + } + if(heights[tempX][tempY] < heights[node[0]][node[1]]) { + continue; + } + + visited[tempX][tempY] = true; + que.add(new int[]{tempX, tempY}); + } + } + } + + public boolean isOutOfIndex(int x, int y){ + return x < 0 || x >= N || y < 0 || y >= M; + } +} diff --git a/unique-paths/se6816.java b/unique-paths/se6816.java new file mode 100644 index 0000000000..cb3e2d0f23 --- /dev/null +++ b/unique-paths/se6816.java @@ -0,0 +1,48 @@ +class Solution { + public int[] moveX = {0, 1}; + public int[] moveY = {1, 0}; + int N; + int M; + public int uniquePaths(int m, int n) { + int[][] dp = new int[m][n]; + boolean[][] visited = new boolean[m][n]; + int curX = 0; + int curY = 0; + N = n; + M = m; + dp[curX][curY] = 1; + visited[curX][curY] = true; + Queue que = new ArrayDeque<>(); + que.offer(new int[]{0,0}); + + while(!que.isEmpty()) { + int[] data = que.poll(); + + for(int i = 0; i < 2; i++) { + int tempX = data[0] + moveX[i]; + int tempY = data[1] + moveY[i]; + + if(isOutOfIndex(tempX, tempY)) { + continue; + } + + dp[tempX][tempY] += dp[data[0]][data[1]]; + + + if(visited[tempX][tempY]) { + continue; + } + visited[tempX][tempY] = true; + que.add(new int[]{tempX, tempY}); + + } + } + + return dp[M-1][N-1]; + + } + + public boolean isOutOfIndex(int x, int y) { + return x < 0 || x >= M || y < 0 || y >=N; + } +}