Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[donghyeon95] Week 7 #953

Merged
merged 6 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions longest-substring-without-repeating-characters/donghyeon95.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.util.HashMap;

class Solution {
public int lengthOfLongestSubstring(String s) {
// subString을 찾아야 한다.
// for 문을 반복하면서 내가 가지고 있는 문자열에 있는 문자라면
// 그 문자열까지의 길이를 기록하고 꼬리를 짜르고 다시 반복문
int result = 0;
HashMap<String, Boolean> 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);
}
}



36 changes: 36 additions & 0 deletions number-of-islands/donghyeon95.java
Original file line number Diff line number Diff line change
@@ -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.length; i++) {
for (int j=0; j<grid[0].length; j++) {
if (grid[i][j] == '1' && !visited[i][j]) {
dfs(i, j, grid);
result++;
}
}
}

return result;
}

public void dfs(int y, int x, char[][] grid) {
visited[y][x] = true;

for (int[] move: moves) {
int newX = x + move[0];
int newY = y + move[1];
Comment on lines +27 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오.. 이러면 테두리 경계값을 따로 확인하지 않아도 넘어갈 일이 없겠군요..!


if (newX<0 || newX >= grid[0].length || newY<0 || newY >= grid.length || visited[newY][newX] || grid[newY][newX]=='0') continue;
dfs(newY, newX, grid);
}
}
}


30 changes: 30 additions & 0 deletions reverse-linked-list/donghyeon95.java
Original file line number Diff line number Diff line change
@@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ListNode nextNode = head.next 로 한다면 공간 복잡도를 O(1) 으로 줄일 수 있지 않을까요?

nextNode.next = result;
result = nextNode;
head = head.next;
}

return result;
}
}


58 changes: 58 additions & 0 deletions set-matrix-zeroes/donghyeon95.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}


21 changes: 21 additions & 0 deletions unique-paths/donghyeon95.java
Original file line number Diff line number Diff line change
@@ -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<m; i++) {
dp[i][0] = 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];
}
}

Loading