Skip to content

Commit 1a71772

Browse files
add: [week-11] missing-number, reorder-list
1 parent 8c060a0 commit 1a71772

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

missing-number/YoungSeok-Choi.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
class Solution {
3+
public int missingNumber(int[] nums) {
4+
5+
int len = nums.length;
6+
Map<Integer, Boolean> nMap = new HashMap<>();
7+
8+
for (int i = 0; i <= len; i++) {
9+
nMap.put(i, true);
10+
}
11+
12+
for (int anInt : nums) {
13+
nMap.remove(anInt);
14+
}
15+
16+
List<Integer> keyList = new ArrayList<>(nMap.keySet());
17+
18+
return keyList.get(0);
19+
}
20+
}

reorder-list/YoungSeok-Choi.java

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
public class ListNode {
2+
int val;
3+
ListNode next;
4+
5+
ListNode() {
6+
}
7+
8+
ListNode(int val) {
9+
this.val = val;
10+
}
11+
12+
ListNode(int val, ListNode next) {
13+
this.val = val;
14+
this.next = next;
15+
}
16+
}
17+
18+
class Solution {
19+
public void reorderList(ListNode head) {
20+
List<Integer> nArr = new ArrayList<>();
21+
Map<Integer, List<ListNode>> lMap = new HashMap<>();
22+
23+
ListNode cur = head.next;
24+
while (cur != null) {
25+
ListNode next = cur.next;
26+
cur.next = null;
27+
nArr.add(cur.val);
28+
29+
if (lMap.containsKey(cur.val)) {
30+
lMap.get(cur.val).add(cur);
31+
} else {
32+
List<ListNode> temp = new ArrayList<>();
33+
temp.add(cur);
34+
lMap.put(cur.val, temp);
35+
}
36+
37+
cur = next;
38+
}
39+
40+
int[] arr = nArr.stream().mapToInt(i -> i).toArray();
41+
int start = 0;
42+
int end = arr.length - 1;
43+
44+
while (start < end) {
45+
head.next = lMap.get(arr[end]).remove(0);
46+
head = head.next;
47+
head.next = lMap.get(arr[start]).remove(0);
48+
head = head.next;
49+
50+
start++;
51+
end--;
52+
}
53+
54+
if (arr.length % 2 != 0) {
55+
for (int anInt : new ArrayList<>(lMap.keySet())) {
56+
List<ListNode> anArr = lMap.get(anInt);
57+
58+
if (anArr.size() > 0) {
59+
head.next = anArr.remove(0);
60+
}
61+
}
62+
}
63+
}
64+
}
65+
66+
// NOTE: 동일한 val의 ListNode를 고려하지 못하여 틀린 경우
67+
class WrongSolution {
68+
public void reorderList(ListNode head) {
69+
List<Integer> nArr = new ArrayList<>();
70+
Map<Integer, ListNode> lMap = new HashMap<>();
71+
72+
ListNode cur = head.next;
73+
while (cur != null) {
74+
ListNode next = cur.next;
75+
cur.next = null;
76+
nArr.add(cur.val);
77+
lMap.put(cur.val, cur);
78+
cur = next;
79+
}
80+
81+
int[] arr = nArr.stream().mapToInt(i -> i).toArray();
82+
int start = 0;
83+
int end = arr.length - 1;
84+
85+
while (start < end) {
86+
head.next = lMap.remove(arr[end]);
87+
head = head.next;
88+
head.next = lMap.remove(arr[start]);
89+
head = head.next;
90+
91+
start++;
92+
end--;
93+
}
94+
95+
if (arr.length % 2 != 0) {
96+
for (int anInt : new ArrayList<>(lMap.keySet())) {
97+
head.next = lMap.remove(anInt);
98+
}
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)