File tree Expand file tree Collapse file tree 3 files changed +116
-0
lines changed
Expand file tree Collapse file tree 3 files changed +116
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } nums
3+ * @return {number[][] }
4+ */
5+ var threeSum = function ( nums ) {
6+ const answer = [ ] ;
7+
8+ const sorted = nums . sort ( ( a , b ) => a - b ) ;
9+
10+ for ( let i = 0 ; i < sorted . length ; i ++ ) {
11+ if ( i > 0 && sorted [ i ] === sorted [ i - 1 ] ) {
12+ // ์ ์กฐ๊ฑด์ผ๋ก ์ค๋ณต ์ซ์ ํํฐ
13+ continue ;
14+ }
15+
16+ let l = i + 1 ;
17+ let h = sorted . length - 1 ;
18+
19+ while ( l < h ) {
20+ const sum = sorted [ i ] + sorted [ l ] + sorted [ h ] ;
21+
22+ if ( sum === 0 ) {
23+ const arr = [ sorted [ i ] , sorted [ l ] , sorted [ h ] ] ;
24+ answer . push ( arr ) ;
25+
26+ // ์๋ ๋ฐ๋ณต๋ฌธ์ผ๋ก ์ค๋ณต ์ซ์ ํํฐ
27+ while ( l < h && sorted [ l ] === sorted [ l + 1 ] ) l ++ ;
28+ while ( l < h && sorted [ h ] === sorted [ h - 1 ] ) h -- ;
29+
30+ h -- ;
31+ l ++ ;
32+ }
33+
34+ if ( sum > 0 ) h -- ;
35+ if ( sum < 0 ) l ++ ;
36+ }
37+ }
38+
39+ return answer ;
40+ } ;
41+
42+ // TC: O(n2)
43+ // SC: O(1)
44+
45+
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number } n
3+ * @return {number }
4+ */
5+ var climbStairs = function ( n ) {
6+ let first = 1 ;
7+ let second = 2 ;
8+
9+ if ( n <= 2 ) {
10+ return n ;
11+ }
12+
13+ for ( let i = 2 ; i < n ; i ++ ) {
14+ let tmp = second ;
15+ second = first + second ;
16+ first = tmp ;
17+ }
18+
19+ return second ;
20+ } ;
21+
22+
Original file line number Diff line number Diff line change 1+ const getDictionary = ( s ) => {
2+ const arr = s . split ( '' ) ;
3+
4+ const dict = { } ;
5+
6+ for ( let i = 0 ; i < arr . length ; i ++ ) {
7+ const key = arr [ i ] ;
8+ const value = dict [ key ] ;
9+
10+ if ( value === undefined ) {
11+ dict [ key ] = 1 ;
12+ } else {
13+ dict [ key ] = dict [ key ] + 1 ;
14+ }
15+ }
16+
17+ return dict ;
18+ }
19+
20+ const checkSameLength = ( dictA , dictB ) => {
21+ return Object . keys ( dictA ) . length === Object . keys ( dictB ) . length
22+ }
23+
24+ const checkSameDict = ( s , t ) => {
25+ for ( const key in s ) {
26+ if ( s [ key ] !== t [ key ] ) {
27+ return false ;
28+ }
29+ }
30+
31+ return true ;
32+ }
33+
34+ /**
35+ * @param {string } s
36+ * @param {string } t
37+ * @return {boolean }
38+ */
39+ var isAnagram = function ( s , t ) {
40+ const dictA = getDictionary ( s ) ;
41+
42+ const dictB = getDictionary ( t ) ;
43+
44+ return checkSameLength ( dictA , dictB ) && checkSameDict ( dictA , dictB ) ;
45+ } ;
46+
47+ // ๊ณต๊ฐ๋ณต์ก๋: ํด์ ํ
์ด๋ธ์ ๋ชจ๋ ๋ฌธ์๋ฅผ ์ ์ฅํ๊ฒ ๋๋ฏ๋ก O(n)
48+ // ์๊ฐ๋ณต์ก๋: ๋ ๊ฐ์ ๋ฌธ์์ด์ ํ ๋ฒ์ฉ ๋ฃจํ๋ฅผ ๋๊ณ ์๊ธฐ ๋๋ฌธ์ 0(n)
49+
You canโt perform that action at this time.
0 commit comments