Skip to content

Commit f0c3729

Browse files
authored
Merge pull request #916 from eunhwa99/main
[eunhwa99] week 7
2 parents 6d1380b + eece4a7 commit f0c3729

File tree

5 files changed

+189
-0
lines changed

5 files changed

+189
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
class Solution {
5+
// 시간 복잡도: O(N)
6+
// 공간 복잡도: O(N)
7+
public int lengthOfLongestSubstring(String s) {
8+
Map<Character, Integer> position = new HashMap<>();
9+
int start = 0; // substring의 시작점
10+
int maxLength = 0;
11+
12+
for (int idx = 0; idx < s.length(); idx++) {
13+
char currentChar = s.charAt(idx);
14+
15+
// 같은 문자가 이미 map 에 있고, 그 문자가 현재 substring에 포함된 문자인지 확인
16+
if (position.containsKey(currentChar) && position.get(currentChar) >= start) {
17+
start = position.get(currentChar) + 1;
18+
// 같은 문자가 포함되지 않게 substring의 시작을 옮긴다.
19+
}
20+
21+
maxLength = Math.max(maxLength, idx - start + 1);
22+
23+
position.put(currentChar, idx);
24+
}
25+
26+
return maxLength;
27+
}
28+
}
29+

number-of-islands/eunhwa99.java

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import java.util.LinkedList;
2+
import java.util.Queue;
3+
4+
// BFS 사용
5+
// 시간 복잡도 : O(MxN)
6+
// 공간 복잡도: O(MxN)
7+
class Solution {
8+
9+
int[][] dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
10+
11+
public int numIslands(char[][] grid) {
12+
int row = grid.length;
13+
int col = grid[0].length;
14+
15+
int total = 0;
16+
for (int i = 0; i < row; i++) {
17+
for (int j = 0; j < col; j++) {
18+
if (grid[i][j] == '1') {
19+
total++;
20+
BFS(grid, i, j, row, col);
21+
System.out.println(grid[i][j]);
22+
}
23+
}
24+
}
25+
return total;
26+
}
27+
28+
private void BFS(char[][] grid, int r, int c, int sizeR, int sizeC) {
29+
Queue<Position> queue = new LinkedList<>();
30+
31+
queue.add(new Position(r, c));
32+
grid[r][c] = '0'; // '0'으로 변경 (방문 체크)
33+
34+
while (!queue.isEmpty()) {
35+
Position current = queue.poll();
36+
int curR = current.r;
37+
int curC = current.c;
38+
39+
for (int i = 0; i < 4; i++) {
40+
int dirR = dir[i][0];
41+
int dirC = dir[i][1];
42+
43+
int nextR = curR + dirR;
44+
int nextC = curC + dirC;
45+
46+
if (nextR < 0 || nextR >= sizeR || nextC < 0 || nextC >= sizeC || grid[nextR][nextC] == '0') {
47+
continue;
48+
}
49+
queue.add(new Position(nextR, nextC));
50+
grid[nextR][nextC] = '0';
51+
}
52+
}
53+
54+
}
55+
56+
static class Position {
57+
58+
int r;
59+
int c;
60+
61+
Position(int r, int c) {
62+
this.r = r;
63+
this.c = c;
64+
}
65+
}
66+
}
67+

reverse-linked-list/eunhwa99.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
if(head==null) return head;
17+
18+
ListNode pointer = new ListNode(head.val);
19+
20+
ListNode tempPointer;
21+
while(head.next!=null){
22+
tempPointer = new ListNode(head.next.val, pointer);
23+
pointer = tempPointer;
24+
head = head.next;
25+
}
26+
}
27+
return pointer;
28+
}
29+

set-matrix-zeroes/eunhwa99.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
// 시간 복잡도: O(row x col)
5+
// 공간 복잡도: O(row + col)
6+
class Solution {
7+
8+
public void setZeroes(int[][] matrix) {
9+
int row = matrix.length;
10+
int col = matrix[0].length;
11+
12+
// 행과 열에 0이 있는지 체크할 배열
13+
Set<Integer> rowZero = new HashSet<>();
14+
Set<Integer> colZero = new HashSet<>();
15+
16+
for (int i = 0; i < row; i++) {
17+
for (int j = 0; j < col; j++) {
18+
if (matrix[i][j] == 0) {
19+
rowZero.add(i);
20+
colZero.add(j);
21+
}
22+
}
23+
}
24+
25+
// 행을 0으로 설정
26+
for (int r : rowZero) {
27+
for (int c = 0; c < col; c++) {
28+
matrix[r][c] = 0;
29+
}
30+
}
31+
32+
// 열을 0으로 설정
33+
for (int c : colZero) {
34+
for (int r = 0; r < row; r++) {
35+
matrix[r][c] = 0;
36+
}
37+
}
38+
}
39+
}
40+
41+

unique-paths/eunhwa99.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
3+
public int uniquePaths(int m, int n) {
4+
int[][] paths = new int[m][n];
5+
6+
for (int i = 0; i < m; i++) {
7+
paths[i][0] = 1; //가장 왼쪽 줄은 항상 경로가 1개
8+
}
9+
10+
for (int i = 0; i < n; i++) {
11+
paths[0][i] = 1; // 가장 윗줄은 항상 경로가 1개
12+
}
13+
for (int i = 1; i < m; i++) {
14+
for (int j = 1; j < n; j++) {
15+
16+
paths[i][j] = paths[i - 1][j] + paths[i][j - 1];
17+
}
18+
}
19+
20+
return paths[m - 1][n - 1];
21+
}
22+
}
23+

0 commit comments

Comments
 (0)