File tree Expand file tree Collapse file tree 4 files changed +135
-0
lines changed
construct-binary-tree-from-preorder-and-inorder-traversal Expand file tree Collapse file tree 4 files changed +135
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * ์๊ฐ ๋ณต์ก๋:
3
+ * nums์ ๊ธธ์ด๋งํผ for๋ฌธ์ ์ํํ๊ณ , ๋ด๋ถ์์ ํฌ ํฌ์ธํฐ๋ก ๋ ํ ๋ฒ ์ํํ๋ฏ๋ก, O(nยฒ)
4
+ * ๊ณต๊ฐ ๋ณต์ก๋:
5
+ * ์ ๋ ฌ์ ์ถ๊ฐ ๊ณต๊ฐ ์ฌ์ฉ์ด ์์.
6
+ * res ๋ฐฐ์ด์ ํฌ๊ธฐ๋ ๊ณ ์ ํ ์ธ ์ซ์ ์กฐํฉ์ ๊ฐฏ์.
7
+ * ์ด๋ฅผ k๋ผ๊ณ ํ๋ฉด, ๊ณต๊ฐ ๋ณต์ก๋๋ O(k)
8
+ */
9
+ /**
10
+ * @param {number[] } nums
11
+ * @return {number[][] }
12
+ */
13
+ var threeSum = function ( nums ) {
14
+ nums . sort ( ( a , b ) => a - b ) ;
15
+
16
+ const res = [ ] ;
17
+ for ( let i = 0 ; i < nums . length ; i ++ ) {
18
+ if ( i > 0 && nums [ i ] === nums [ i - 1 ] ) {
19
+ continue ;
20
+ }
21
+
22
+ let l = i + 1 ;
23
+ let r = nums . length - 1 ;
24
+ while ( l < r ) {
25
+ const sum = nums [ i ] + nums [ l ] + nums [ r ] ;
26
+ if ( sum > 0 ) {
27
+ r -- ;
28
+ } else if ( sum < 0 ) {
29
+ l ++ ;
30
+ } else if ( sum === 0 ) {
31
+ res . push ( [ nums [ i ] , nums [ l ] , nums [ r ] ] ) ;
32
+ l ++ ;
33
+ r -- ;
34
+ while ( l < r && nums [ l ] === nums [ l - 1 ] ) {
35
+ l ++ ;
36
+ }
37
+ }
38
+ }
39
+ }
40
+ return res ;
41
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * ์๊ฐ ๋ณต์ก๋:
3
+ * ๋ฉ๋ชจ์ด์ ์ด์
์ ์ฌ์ฉํ์ฌ n๊น์ง ๊ฐ๊ธฐ ์ํ ๋ฐฉ๋ฒ์ ์๋ฅผ ์ ์ฅ.
4
+ * ์ฌ๊ท ํจ์๋ ์ต๋ n๋งํผ ํธ์ถ๋จ.
5
+ * ๋ฐ๋ผ์, ์๊ฐ ๋ณต์ก๋๋ O(n)
6
+ * ๊ณต๊ฐ ๋ณต์ก๋:
7
+ * ๋ฉ๋ชจ ๊ฐ์ฒด์ ํฌ๊ธฐ๋ n์ ํฌ๊ธฐ์ ๊ฐ์.
8
+ * ์ฌ๊ท ํจ์๋ ์ต๋ n๋งํผ ํธ์ถ๋จ.
9
+ * ๋ฐ๋ผ์, ๊ณต๊ฐ ๋ณต์ก๋๋ O(n)
10
+ */
11
+ /**
12
+ * @param {number } n
13
+ * @return {number }
14
+ */
15
+ var climbStairs = function ( n ) {
16
+ const memo = [ 0 , 1 , 2 ] ;
17
+ const recurse = ( n ) => {
18
+ if ( memo [ n ] ) {
19
+ return memo [ n ] ;
20
+ }
21
+ memo [ n ] = recurse ( n - 1 ) + recurse ( n - 2 ) ;
22
+ return memo [ n ] ;
23
+ }
24
+ return recurse ( n ) ;
25
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * ์๊ฐ ๋ณต์ก๋:
3
+ * preIdx๋ฅผ ์ด์ฉํ์ฌ ์ค์ ์ํ ๋ฐฐ์ด์์ ๋ฃจํธ ๋
ธ๋๋ฅผ ๊ธฐ์ค์ผ๋ก ์ผ์ชฝ ์๋ธ ํธ๋ฆฌ์ ์ค๋ฅธ์ชฝ ์๋ธ ํธ๋ฆฌ๋ฅผ ํ์.
4
+ * ๊ฐ ์๋ธ ํธ๋ฆฌ๋ฅผ ์ฌ๊ท์ ์ผ๋ก ์์ฑํ๋ฉฐ ๋ชจ๋ ๋
ธ๋๋ฅผ ํ ๋ฒ์ฉ ์ํํ๋ฏ๋ก, ์๊ฐ ๋ณต์ก๋๋ O(n)
5
+ * ๊ณต๊ฐ ๋ณต์ก๋:
6
+ * ์ค์ ์ํ ๋ฐฐ์ด์ ๊ธธ์ด๋งํผ ๋งต์ ์์ฑํ๋ฏ๋ก, ๊ณต๊ฐ ๋ณต์ก๋๋ O(n)
7
+ */
8
+ /**
9
+ * Definition for a binary tree node.
10
+ * function TreeNode(val, left, right) {
11
+ * this.val = (val===undefined ? 0 : val)
12
+ * this.left = (left===undefined ? null : left)
13
+ * this.right = (right===undefined ? null : right)
14
+ * }
15
+ */
16
+ /**
17
+ * @param {number[] } preorder
18
+ * @param {number[] } inorder
19
+ * @return {TreeNode }
20
+ */
21
+ var buildTree = function ( preorder , inorder ) {
22
+ let preIdx = 0 ;
23
+ const inorderMap = new Map ( inorder . map ( ( e , i ) => [ e , i ] ) )
24
+
25
+ const dfs = ( l , r ) => {
26
+ if ( l > r ) {
27
+ return null ;
28
+ }
29
+ let root = preorder [ preIdx ] ;
30
+ preIdx ++ ;
31
+
32
+ let rootIdx = inorderMap . get ( root ) ;
33
+
34
+ const node = new TreeNode ( root ) ;
35
+ node . left = dfs ( l , rootIdx - 1 ) ;
36
+ node . right = dfs ( rootIdx + 1 , r ) ;
37
+ return node ;
38
+ }
39
+ return dfs ( 0 , inorder . length - 1 )
40
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * ์๊ฐ ๋ณต์ก๋:
3
+ * s์ t์ ๊ธธ์ด๋งํผ ๊ฐ ๋ฌธ์์ ์นด์ดํธ๋ฅผ ๊ธฐ๋กํ๊ณ ์ด๋ฅผ ํ์ธํ๋ฏ๋ก, ์๊ฐ ๋ณต์ก๋๋ O(n)
4
+ * ๊ณต๊ฐ ๋ณต์ก๋:
5
+ * ์นด์ดํธ ๊ฐ์ฒด๋ ์ต๋ s์ t์ ๊ธธ์ด๋งํผ ๊ณต๊ฐ์ ์ฐจ์งํ๋ฏ๋ก, ๊ณต๊ฐ ๋ณต์ก๋๋ O(n)
6
+ */
7
+ /**
8
+ * @param {string } s
9
+ * @param {string } t
10
+ * @return {boolean }
11
+ */
12
+ var isAnagram = function ( s , t ) {
13
+ if ( s . length !== t . length ) {
14
+ return false ;
15
+ }
16
+
17
+ const count = { } ;
18
+ for ( let i = 0 ; i < s . length ; i ++ ) {
19
+ count [ s [ i ] ] = ( count [ s [ i ] ] || 0 ) + 1 ;
20
+ count [ t [ i ] ] = ( count [ t [ i ] ] || 0 ) - 1 ;
21
+ }
22
+
23
+ for ( const key in count ) {
24
+ if ( count [ key ] !== 0 ) {
25
+ return false ;
26
+ }
27
+ }
28
+ return true ;
29
+ } ;
You canโt perform that action at this time.
0 commit comments