File tree Expand file tree Collapse file tree 3 files changed +79
-23
lines changed
product-of-array-except-self Expand file tree Collapse file tree 3 files changed +79
-23
lines changed Original file line number Diff line number Diff line change 33 * @return {number }
44 */
55var climbStairs = function ( n ) {
6- //(n) = f(n - 1) + f(n - 2)
7- let tempArray = [ ] ;
8-
9- for ( let i = 0 ; i <= n ; i ++ ) {
10- if ( i === 0 || i === 1 ) {
11- tempArray . push ( 1 ) ;
12- } else {
13- let tempSum = 0 ;
14- tempSum = tempArray [ i - 2 ] + tempArray [ i - 1 ] ;
15- tempArray . push ( tempSum ) ;
16- }
6+
7+ // ways(n) = ways(n-1) + ways(n-2)
8+
9+ const waysArray = new Array ( n ) ;
10+
11+ waysArray [ 0 ] = 1 ; //실제 1번째 계단까지 오르는 경우의 수
12+ waysArray [ 1 ] = 2 ; //실제 2번째 계단까지 오르는 경우의 수
13+
14+ for ( let i = 2 ; i < n ; i ++ ) {
15+ waysArray [ i ] = waysArray [ i - 1 ] + waysArray [ i - 2 ] ;
1716 }
18- return tempArray [ n ] ;
17+
18+ return waysArray [ n - 1 ] ;
19+
1920} ;
21+
22+ //------------1회차 풀이-----------------
23+ // /**
24+ // * @param {number } n
25+ // * @return {number }
26+ // */
27+ // var climbStairs = function(n) {
28+ // //(n) = f(n - 1) + f(n - 2)
29+ // let tempArray = [];
30+
31+ // for(let i = 0; i <= n; i++){
32+ // if(i === 0 || i === 1){
33+ // tempArray.push(1);
34+ // }else{
35+ // let tempSum = 0;
36+ // tempSum = tempArray[i - 2] + tempArray[i - 1];
37+ // tempArray.push(tempSum);
38+ // }
39+ // }
40+ // return tempArray[n];
41+ // };
42+
Original file line number Diff line number Diff line change 44 */
55var productExceptSelf = function ( nums ) {
66
7+ const prefixProductArray = new Array ( nums . length ) ;
8+ const suffixProductArray = new Array ( nums . length ) ;
9+ const totalProductArray = new Array ( nums . length ) ;
10+
11+ //prefixProductArray의 기본값 설정
12+ prefixProductArray [ 0 ] = 1 ;
13+ prefixProductArray [ 1 ] = nums [ 0 ] ;
14+
15+ //suffixProductArray의 기본값 설정
16+ suffixProductArray [ nums . length - 1 ] = 1 ;
17+ suffixProductArray [ nums . length - 2 ] = nums [ nums . length - 1 ] ;
18+
19+ for ( let i = 2 ; i < nums . length ; i ++ ) {
20+ prefixProductArray [ i ] = nums [ i - 1 ] * prefixProductArray [ i - 1 ] ;
21+ }
22+
23+ for ( let i = nums . length - 3 ; i >= 0 ; i -- ) {
24+ suffixProductArray [ i ] = nums [ i + 1 ] * suffixProductArray [ i + 1 ] ;
25+ }
26+
27+ for ( let i = 0 ; i < nums . length ; i ++ ) {
28+ totalProductArray [ i ] = prefixProductArray [ i ] * suffixProductArray [ i ] ;
29+ }
30+
31+ return totalProductArray ;
32+
33+ } ;
34+
35+ //-------------1회차 풀이(6기)-------------
36+ /*
37+ var productExceptSelf = function (nums) {
38+
739 const NUMS_LENGTH = nums.length;
840 const isZeroArray = []; //boolean
941 let zeroCount = 0;
@@ -40,4 +72,7 @@ var productExceptSelf = function (nums) {
4072
4173 return tempArray;
4274};
75+ */
76+
77+
4378
Original file line number Diff line number Diff line change 44 * @return {boolean }
55 */
66var isAnagram = function ( s , t ) {
7- //1. s와 t를 Array로 만든다.
8- const sArray = s . split ( "" ) ;
9- const tArray = t . split ( "" ) ;
7+ const counter = new Array ( 26 ) . fill ( 0 ) ;
108
11- //2. sArray와 tArray의 sort를 같게 만든다.
12- const sortedSArray = sArray . sort ( ) ;
13- const sortedTArray = tArray . sort ( ) ;
9+ //edge case: s와 t의 길이가 다른 경우
10+ if ( s . length !== t . length ) return false ;
1411
15- //3. sArray와 tArray가 같은지 판별한다.
16- const result = JSON . stringify ( sortedSArray ) === JSON . stringify ( sortedTArray ) ;
17-
18- //4. 같으면 true를, 다르면 false를 반환한다.
19- return result ;
12+ for ( let i = 0 ; i < s . length ; i ++ ) {
13+ counter [ s . charCodeAt ( i ) - 97 ] ++ ;
14+ counter [ t . charCodeAt ( i ) - 97 ] -- ;
15+ }
2016
17+ return counter . every ( count => count === 0 ) ;
2118} ;
19+
You can’t perform that action at this time.
0 commit comments