Skip to content

Commit 6680459

Browse files
authored
Merge pull request DaleStudy#2492 from Hyeri1ee/main
[Hyeri1ee] WEEK 04 Solutions
2 parents 8d657e5 + 514a084 commit 6680459

4 files changed

Lines changed: 153 additions & 0 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//맨뒤 요소 맨앞으로
2+
import java.util.*;
3+
4+
5+
class Solution {
6+
public int findMin(int[] nums) {
7+
Arrays.sort(nums);
8+
return nums[0];
9+
}
10+
}
11+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
import java.util.*;
17+
class Solution {
18+
private static int solve(TreeNode root, int depth){
19+
if (root == null){
20+
return depth;
21+
}
22+
23+
//root가null이 아닌 경우
24+
return Math.max(solve(root.left, depth+1) , solve(root.right, depth+1));
25+
26+
}
27+
public int maxDepth(TreeNode root) {
28+
int result = solve(root, 0);
29+
return result;
30+
}
31+
}
32+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.*;
2+
3+
4+
5+
6+
class Solution {
7+
8+
9+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
10+
ListNode merged = new ListNode();//머지 된 노드 모음
11+
ListNode temp = merged;
12+
while(list1 != null && list2 != null){
13+
14+
ListNode newNode = new ListNode();//새로운 노드
15+
if (list1.val > list2.val){
16+
//list2
17+
newNode.val = list2.val;
18+
list2 =list2.next;//다음 가리키도록\
19+
// merged.next
20+
}else{
21+
newNode.val =list1.val;
22+
list1=list1.next;//다음 가리키도록
23+
}
24+
25+
merged.next = newNode;
26+
merged =merged.next;
27+
28+
}
29+
30+
if (list1==null)
31+
merged.next= list2;
32+
else if (list2 == null)
33+
merged.next=list1;
34+
35+
return temp.next;
36+
}
37+
}

word-search/Hyeri1ee.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import java.util.*;
2+
3+
4+
class Solution {
5+
6+
//상,하,좌,우
7+
static int[] dx = {0,1,-1,0};
8+
static int[] dy = {1,0,0,-1};
9+
10+
static int m, n;
11+
12+
static boolean result; //exist 함수가 호출될때마다 초기화해야함
13+
static char[][] map; //지역변수 board 저장용
14+
static String w; //word 저장용
15+
public boolean exist(char[][] board, String word) {
16+
17+
result=false;
18+
19+
m = board.length;
20+
n = board[0].length;
21+
22+
w = word;
23+
//map <- board
24+
map = new char[m][n];
25+
for(int i =0; i<m;i++){
26+
for(int j=0; j< n; j++){
27+
map[i][j] = board[i][j];
28+
}
29+
}
30+
31+
32+
for(int x =0 ; x < m; x++){
33+
for(int y =0 ; y < n ;y++){
34+
dfs(x, y , 0); //위치 x,y, word에 대한 찾는 인덱스
35+
if (result) return result;
36+
}
37+
}
38+
39+
40+
41+
//기본
42+
return result;
43+
}
44+
45+
private static void dfs(int x, int y, int idx){
46+
//종료조건
47+
if (result) return;
48+
49+
if (!checkin(x, y) || map[x][y] != w.charAt(idx)) return;
50+
51+
//마지막 문자일때 종료
52+
if (idx == w.length() - 1){
53+
result = true;
54+
return;
55+
}
56+
57+
char temp = map[x][y];
58+
map[x][y] = '#';
59+
60+
for(int i = 0; i < 4; i++){
61+
dfs(x + dx[i], y + dy[i], idx + 1);
62+
}
63+
64+
map[x][y] = temp;
65+
66+
}
67+
68+
//map 내부 checkin 함수 정의
69+
private static boolean checkin(int x, int y){
70+
return x >= 0 && x < m && y >= 0 && y < n;
71+
}
72+
}
73+

0 commit comments

Comments
 (0)