File tree Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * source: https://leetcode.com/problems/merge-two-sorted-lists/
3
+ * ํ์ด๋ฐฉ๋ฒ: ๋ ๋ฆฌ์คํธ๋ฅผ ๋น๊ตํ๋ฉด์ ์์ ๊ฐ์ ๊ฒฐ๊ณผ ๋ฆฌ์คํธ์ ์ถ๊ฐ
4
+ *
5
+ * ์๊ฐ๋ณต์ก๋: O(n + m) (n: list1์ ๊ธธ์ด, m: list2์ ๊ธธ์ด)
6
+ * ๊ณต๊ฐ๋ณต์ก๋: O(1) (์์ ๊ณต๊ฐ๋ง ์ฌ์ฉ)
7
+ *
8
+ */
9
+
10
+ class ListNode {
11
+ val : number ;
12
+ next : ListNode | null ;
13
+ constructor ( val ?: number , next ?: ListNode | null ) {
14
+ this . val = val === undefined ? 0 : val ;
15
+ this . next = next === undefined ? null : next ;
16
+ }
17
+ }
18
+
19
+ function mergeTwoLists (
20
+ list1 : ListNode | null ,
21
+ list2 : ListNode | null
22
+ ) : ListNode | null {
23
+ const result = new ListNode ( ) ;
24
+ let current = result ;
25
+ while ( list1 !== null && list2 !== null ) {
26
+ if ( list1 . val <= list2 . val ) {
27
+ current . next = list1 ;
28
+ list1 = list1 . next ;
29
+ current = current . next ;
30
+ } else {
31
+ current . next = list2 ;
32
+ list2 = list2 . next ;
33
+ current = current . next ;
34
+ }
35
+ }
36
+ if ( list1 !== null ) {
37
+ current . next = list1 ;
38
+ }
39
+ if ( list2 !== null ) {
40
+ current . next = list2 ;
41
+ }
42
+ return result . next ; // ์ฒ์์ ์ถ๊ฐํ ๋๋ฏธ ๋
ธ๋ ์ ์ธ
43
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * source: https://leetcode.com/problems/missing-number/
3
+ * ํ์ด๋ฐฉ๋ฒ: 0๋ถํฐ n๊น์ง์ ํฉ์์ ์ฃผ์ด์ง ๋ฐฐ์ด์ ํฉ์ ๋นผ๋ฉด ๋น ์ง ์ซ์๋ฅผ ๊ตฌํ ์ ์์
4
+ *
5
+ * ์๊ฐ๋ณต์ก๋: O(n) (n: nums์ ๊ธธ์ด)
6
+ * ๊ณต๊ฐ๋ณต์ก๋: O(1) (์์ ๊ณต๊ฐ๋ง ์ฌ์ฉ)
7
+ */
8
+
9
+ function missingNumber ( nums : number [ ] ) : number {
10
+ const n = nums . length ;
11
+ let expectedSum = ( n * ( n + 1 ) ) / 2 ; // 0๋ถํฐ n๊น์ง์ ํฉ ๊ณต์
12
+ let realSum = nums . reduce ( ( sum , cur ) => sum + cur , 0 ) ;
13
+
14
+ return expectedSum - realSum ;
15
+ }
You canโt perform that action at this time.
0 commit comments