Skip to content

Commit 1acaaad

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents ea9b1fd + 9295f4c commit 1acaaad

File tree

96 files changed

+4236
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+4236
-0
lines changed

clone-graph/YoungSeok-Choi.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
/*
5+
// Definition for a Node.
6+
class Node {
7+
public int val;
8+
public List<Node> neighbors;
9+
public Node() {
10+
val = 0;
11+
neighbors = new ArrayList<Node>();
12+
}
13+
public Node(int _val) {
14+
val = _val;
15+
neighbors = new ArrayList<Node>();
16+
}
17+
public Node(int _val, ArrayList<Node> _neighbors) {
18+
val = _val;
19+
neighbors = _neighbors;
20+
}
21+
}
22+
*/
23+
class Solution {
24+
public Map<Node, Node> nMap = new HashMap<>();
25+
26+
public Node cloneGraph(Node node) {
27+
if (node == null)
28+
return null;
29+
30+
if (nMap.containsKey(node)) {
31+
return nMap.get(node);
32+
}
33+
34+
Node clone = new Node(node.val);
35+
nMap.put(node, clone);
36+
37+
for (Node nei : node.neighbors) {
38+
clone.neighbors.add(cloneGraph(nei));
39+
}
40+
41+
return clone;
42+
}
43+
}
44+
45+
class WrongSolution {
46+
public Node graph;
47+
public Map<Node, Node> nMap = new HashMap<>();
48+
49+
public Node cloneGraph(Node node) {
50+
if (node == null)
51+
return null;
52+
53+
graph = new Node(node.val);
54+
nMap.put(node, graph);
55+
56+
// print(graph);
57+
58+
return clone(node, graph);
59+
}
60+
61+
public Node clone(Node node, Node cur) {
62+
for (int i = 0; i < node.neighbors.size(); i++) {
63+
Node adj = node.neighbors.get(i);
64+
65+
if (nMap.containsKey(adj)) {
66+
return nMap.get(adj);
67+
}
68+
69+
nMap.put(adj, new Node(adj.val));
70+
cur.neighbors.add(nMap.get(adj));
71+
clone(adj, nMap.get(adj));
72+
}
73+
74+
return cur;
75+
}
76+
77+
public void print(Node node) {
78+
System.out.println("visit " + node.val);
79+
80+
for (int i = 0; i < node.neighbors.size(); i++) {
81+
Node adj = node.neighbors.get(i);
82+
System.out.println("nei " + adj.val);
83+
print(adj);
84+
}
85+
}
86+
}

clone-graph/byol-han.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* https://leetcode.com/problems/clone-graph/
3+
* // Definition for a _Node.
4+
* function _Node(val, neighbors) {
5+
* this.val = val === undefined ? 0 : val;
6+
* this.neighbors = neighbors === undefined ? [] : neighbors;
7+
* };
8+
* 시간 복잡도: O(N) — 노드 수만큼 순회
9+
* 공간 복잡도: O(N) — visited 맵과 재귀 호출 스택
10+
*/
11+
12+
/**
13+
* @param {_Node} node
14+
* @return {_Node}
15+
*/
16+
var cloneGraph = function (node) {
17+
if (!node) return null;
18+
19+
const visited = new Map();
20+
21+
const dfs = (n) => {
22+
if (visited.has(n)) {
23+
return visited.get(n);
24+
}
25+
26+
const clone = new Node(n.val);
27+
visited.set(n, clone);
28+
29+
for (let neighbor of n.neighbors) {
30+
clone.neighbors.push(dfs(neighbor));
31+
}
32+
33+
return clone;
34+
};
35+
36+
return dfs(node);
37+
};

clone-graph/krokerdile.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {_Node} node
3+
* @return {_Node}
4+
*/
5+
var cloneGraph = function(node) {
6+
if (!node) return null;
7+
8+
const visited = new Map(); // 원본 노드 -> 복제 노드
9+
10+
function dfs(curr) {
11+
if (visited.has(curr)) {
12+
return visited.get(curr); // 이미 복제한 경우
13+
}
14+
15+
const copy = new _Node(curr.val);
16+
visited.set(curr, copy); // 복제한 노드 저장
17+
18+
for (const neighbor of curr.neighbors) {
19+
copy.neighbors.push(dfs(neighbor)); // 이웃도 재귀적으로 복사
20+
}
21+
22+
return copy;
23+
}
24+
25+
return dfs(node);
26+
};
27+

linked-list-cycle/HoonDongKang.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* [Problem]: [141] Linked List Cycle
3+
* (https://leetcode.com/problems/linked-list-cycle/description/)
4+
*/
5+
class ListNode {
6+
val: number;
7+
next: ListNode | null;
8+
constructor(val?: number, next?: ListNode | null) {
9+
this.val = val === undefined ? 0 : val;
10+
this.next = next === undefined ? null : next;
11+
}
12+
}
13+
14+
function hasCycle(head: ListNode | null): boolean {
15+
//시간복잡도 O(n)
16+
//공간복잡도 O(n)
17+
function setFunc(head: ListNode | null): boolean {
18+
if (!head) return false;
19+
const set = new Set<ListNode>();
20+
let currentNode: ListNode = head;
21+
22+
while (currentNode) {
23+
if (set.has(currentNode)) return true;
24+
25+
set.add(currentNode);
26+
currentNode = currentNode.next!;
27+
}
28+
29+
return false;
30+
}
31+
//시간복잡도 O(n)
32+
//공간복잡도 O(1)
33+
function pointerFunc(head: ListNode | null): boolean {
34+
let slow = head;
35+
let fast = head;
36+
while (fast && fast.next) {
37+
slow = slow.next;
38+
fast = fast.next.next;
39+
40+
if (slow === fast) return true;
41+
}
42+
return false;
43+
}
44+
}

linked-list-cycle/Jeehay28.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class ListNode {
2+
val: number;
3+
next: ListNode | null;
4+
constructor(val?: number, next?: ListNode | null) {
5+
this.val = val === undefined ? 0 : val;
6+
this.next = next === undefined ? null : next;
7+
}
8+
}
9+
10+
// TC: O(n)
11+
// SC: O(1)
12+
function hasCycle(head: ListNode | null): boolean {
13+
14+
let fast: ListNode | null = head;
15+
let slow: ListNode | null = head;
16+
17+
while (fast && fast.next) {
18+
fast = fast.next.next;
19+
20+
if (slow !== null) {
21+
slow = slow.next;
22+
} else {
23+
return false;
24+
}
25+
26+
if (fast === slow) return true;
27+
}
28+
29+
return false;
30+
}
31+
32+
33+
// TC: O(n)
34+
// SC: O(n)
35+
/*
36+
function hasCycle(head: ListNode | null): boolean {
37+
38+
let dummy: ListNode | null = head;
39+
const visited = new Set<ListNode>();
40+
41+
while (dummy) {
42+
if (visited.has(dummy)) return true;
43+
44+
visited.add(dummy);
45+
46+
dummy = dummy.next;
47+
}
48+
49+
return false;
50+
}
51+
*/
52+

linked-list-cycle/PDKhan.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
bool hasCycle(ListNode *head) {
4+
ListNode* slow = head;
5+
ListNode* fast = head;
6+
7+
while(fast && fast->next){
8+
slow = slow->next;
9+
fast = fast->next->next;
10+
11+
if(slow == fast)
12+
return true;
13+
}
14+
15+
return false;
16+
}
17+
};

linked-list-cycle/Tessa1217.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) {
7+
* val = x;
8+
* next = null;
9+
* }
10+
* }
11+
*/
12+
/**
13+
* head는 연결 리스트의 헤드다.
14+
* 특정 노드가 리스트 내의 다른 노드와 next 포인트를 통해 연결되어 있을 때 이 리스트에는 사이클이 있다고 할 수 있다.
15+
* pos는 next 포인터와 연결된 노드의 인덱스를 나타내는데 쓰이며 pos는 파라미터로 주어지지 않는다.
16+
* 주어진 리스트에 사이클이 있다면 true를 그렇지 않다면 false를 반환하세요.
17+
* */
18+
public class Solution {
19+
20+
// 시간복잡도: O(n), 공간복잡도: O(1)
21+
public boolean hasCycle(ListNode head) {
22+
if (head == null) {
23+
return false;
24+
}
25+
26+
ListNode next = head;
27+
ListNode furtherNext = head;
28+
29+
while (furtherNext != null && furtherNext.next != null) {
30+
next = next.next;
31+
furtherNext = furtherNext.next.next;
32+
if (next == furtherNext) {
33+
return true;
34+
}
35+
}
36+
37+
return false;
38+
}
39+
40+
// head 없을 때까지 계속 탐색 반복 : 시간복잡도, 공간복잡도 O(n)
41+
// public boolean hasCycle(ListNode head) {
42+
43+
// Set<ListNode> nodeSet = new HashSet<>();
44+
45+
// while (head != null) {
46+
// if (nodeSet.contains(head)) {
47+
// return true;
48+
// }
49+
// nodeSet.add(head);
50+
// head = head.next;
51+
// }
52+
53+
// return false;
54+
55+
// }
56+
}
57+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import java.util.ArrayList;
2+
import java.util.HashMap;
3+
import java.util.HashSet;
4+
import java.util.List;
5+
import java.util.Map;
6+
import java.util.Set;
7+
8+
public class Solution {
9+
public boolean hasCycle(ListNode head) {
10+
Set<ListNode> nSet = new HashSet<>();
11+
12+
while (head != null) {
13+
if (nSet.contains(head)) {
14+
return true;
15+
}
16+
17+
nSet.add(head);
18+
head = head.next;
19+
}
20+
21+
return false;
22+
}
23+
}
24+
25+
/**
26+
* Definition for singly-linked list.
27+
* class ListNode {
28+
* int val;
29+
* ListNode next;
30+
* ListNode(int x) {
31+
* val = x;
32+
* next = null;
33+
* }
34+
* }
35+
*/
36+
// NOTE: 같은 val의 다른 Node의 경우를 고려하지 못했다
37+
class WrongSolution {
38+
public boolean hasCycle(ListNode head) {
39+
Map<Integer, List<Integer>> vMap = new HashMap<>(); // NOTE: val, pos
40+
41+
if (head == null) {
42+
return false;
43+
}
44+
45+
ListNode cur = head;
46+
boolean isCyclick = false;
47+
int pos = -1;
48+
while (cur != null) {
49+
50+
if (vMap.containsKey(cur.val)) {
51+
isCyclick = true;
52+
break;
53+
}
54+
55+
List<Integer> posList = new ArrayList<>();
56+
posList.add(pos++);
57+
vMap.put(cur.val, posList);
58+
cur = head.next;
59+
}
60+
61+
if (isCyclick) {
62+
return true;
63+
} else {
64+
return false;
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)