Skip to content

Commit cc778ac

Browse files
authored
Merge pull request #953 from donghyeon95/main
[donghyeon95] Week 7
2 parents 4e76819 + e30b45e commit cc778ac

File tree

5 files changed

+182
-0
lines changed

5 files changed

+182
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.HashMap;
2+
3+
class Solution {
4+
public int lengthOfLongestSubstring(String s) {
5+
// subString을 찾아야 한다.
6+
// for 문을 반복하면서 내가 가지고 있는 문자열에 있는 문자라면
7+
// 그 문자열까지의 길이를 기록하고 꼬리를 짜르고 다시 반복문
8+
int result = 0;
9+
HashMap<String, Boolean> hm = new HashMap<>();
10+
StringBuilder nowString = new StringBuilder();
11+
12+
for (String charater: s.split("")) {
13+
System.out.println(charater);
14+
if (hm.containsKey(charater)) {
15+
// nowString
16+
int index = nowString.indexOf(charater);
17+
nowString = new StringBuilder(nowString.substring(index+1) + charater);
18+
result = Math.max(nowString.length(), result);
19+
String removedString = nowString.substring(0, index);
20+
// String의 길이는 최대 27 * n
21+
for (String c: removedString.split("")) {
22+
hm.remove(c);
23+
}
24+
}
25+
else {
26+
hm.put(charater, true);
27+
nowString.append(charater);
28+
}
29+
}
30+
31+
32+
return Math.max(nowString.length(), result);
33+
}
34+
}
35+
36+
37+

number-of-islands/donghyeon95.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
3+
int[][] moves = {{1,0}, {-1,0}, {0,1}, {0,-1}};
4+
boolean[][] visited;
5+
public int numIslands(char[][] grid) {
6+
// 섬의 갯수 => 0혹은 경계선으로 둘러싸인 것의 갯수
7+
// dfs로 탐색
8+
int result = 0;
9+
visited = new boolean[grid.length][grid[0].length];
10+
11+
for (int i=0; i<grid.length; i++) {
12+
for (int j=0; j<grid[0].length; j++) {
13+
if (grid[i][j] == '1' && !visited[i][j]) {
14+
dfs(i, j, grid);
15+
result++;
16+
}
17+
}
18+
}
19+
20+
return result;
21+
}
22+
23+
public void dfs(int y, int x, char[][] grid) {
24+
visited[y][x] = true;
25+
26+
for (int[] move: moves) {
27+
int newX = x + move[0];
28+
int newY = y + move[1];
29+
30+
if (newX<0 || newX >= grid[0].length || newY<0 || newY >= grid.length || visited[newY][newX] || grid[newY][newX]=='0') continue;
31+
dfs(newY, newX, grid);
32+
}
33+
}
34+
}
35+
36+

reverse-linked-list/donghyeon95.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
12+
// 시간 복잡도 : O(n)
13+
// 공간 복잡도 : O(n)
14+
class Solution {
15+
public ListNode reverseList(ListNode head) {
16+
// 반복문을 돌면서 f(x+1)의 next를 f(x)로 지정
17+
ListNode result = null;
18+
19+
while(head != null) {
20+
ListNode nextNode = new ListNode(head.val);
21+
nextNode.next = result;
22+
result = nextNode;
23+
head = head.next;
24+
}
25+
26+
return result;
27+
}
28+
}
29+
30+

set-matrix-zeroes/donghyeon95.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
class Solution {
2+
public void setZeroes(int[][] matrix) {
3+
int rows = matrix.length;
4+
int cols = matrix[0].length;
5+
6+
boolean firstRowHasZero = false;
7+
boolean firstColHasZero = false;
8+
9+
// 1. 첫 번째 행과 열에 0이 있는지 확인
10+
for (int i = 0; i < rows; i++) {
11+
if (matrix[i][0] == 0) {
12+
firstColHasZero = true;
13+
break;
14+
}
15+
}
16+
for (int j = 0; j < cols; j++) {
17+
if (matrix[0][j] == 0) {
18+
firstRowHasZero = true;
19+
break;
20+
}
21+
}
22+
23+
// 2. 나머지 행렬에서 0 찾기 (첫 번째 행과 열에 기록)
24+
for (int i = 1; i < rows; i++) {
25+
for (int j = 1; j < cols; j++) {
26+
if (matrix[i][j] == 0) {
27+
matrix[i][0] = 0; // 해당 행 표시
28+
matrix[0][j] = 0; // 해당 열 표시
29+
}
30+
}
31+
}
32+
33+
// 3. 첫 번째 행과 열의 정보를 기반으로 행렬 수정
34+
for (int i = 1; i < rows; i++) {
35+
for (int j = 1; j < cols; j++) {
36+
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
37+
matrix[i][j] = 0;
38+
}
39+
}
40+
}
41+
42+
// 4. 첫 번째 열 복구
43+
if (firstColHasZero) {
44+
for (int i = 0; i < rows; i++) {
45+
matrix[i][0] = 0;
46+
}
47+
}
48+
49+
// 5. 첫 번째 행 복구
50+
if (firstRowHasZero) {
51+
for (int j = 0; j < cols; j++) {
52+
matrix[0][j] = 0;
53+
}
54+
}
55+
}
56+
}
57+
58+

unique-paths/donghyeon95.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.util.Arrays;
2+
3+
class Solution {
4+
public int uniquePaths(int m, int n) {
5+
// f(x,y) = f(x-1, y) + f(x, y-1)
6+
int[][] dp = new int[m][n];
7+
Arrays.fill(dp[0], 1);
8+
for (int i=0; i<m; i++) {
9+
dp[i][0] = 1;
10+
}
11+
12+
for (int i=1; i<m; i++) {
13+
for (int j=1; j<n; j++) {
14+
dp[i][j] = dp[i-1][j] + dp[i][j-1];
15+
}
16+
}
17+
18+
return dp[m - 1][n - 1];
19+
}
20+
}
21+

0 commit comments

Comments
 (0)