본문 바로가기

전체 글85

Sliding Window function solution(str1, str2) { let i = 0; let j = 0; while (j < str2.length) { if (str2[j] === str1[i]) { i += 1; } //이때 i = 3 if (i === str1.length) { return true; } j++; // j = 0 [a] , i = 0[6] // j = 1 [b] , i = 0[6] // j = 2 [6] , i = 0[6] // j = 3 [C] , i = 1[C] // j = 4 [D] , i = 2[D] // j = 5 [E] , i = 3[] // j = 6 [4] , i = 3[] // j = 7 [4] , i = 3[] // j = 8 [3] , i = 3[] // j = 9 [f] .. 2023. 3. 17.
TIL 20230317(온보딩 10일차) function solution(array, height) { var answer = []; for(i = 0; i height){ answer.push(array[i]) } } return answer.length; } console.log(solution([149,180,192,170],167)) console.log(solution([180,120,140],190)) array 에서 height 보다 큰 수의 숫자를 리턴하면 된다. for 반복문의 조건을 array의 배열 길이만큼 돌게 한 다음, 조건을 달아 만약 array의 i 번째 인덱스가 height보다 크다면 빈 배열을 할당한 answer에 push 메소드를 활용하여 array.. 2023. 3. 17.
[JS] 이중 반복문 성능 개선 방법 function checkSame(arr1, arr2) { let counter1 = {}; let counter2 = {}; for (let item of arr1) { counter1[item] = (counter1[item] || 0) + 1; } for (let item of arr2) { counter2[item] = (counter2[item] || 0) + 1; } for (let key in counter1) { if (!(key in counter2)) { return false; } if (counter2[key] !== counter1[key]) { return false; } } return true; } console.log(checkSame([3, 5, 7, 8, 9], [3, .. 2023. 3. 17.
TIL 20230316(온보딩 9일차) function solution(array, n) { let count = 0 for(i = 0; i < array.length; i++){ if(array[i] === n){ count += 1 } } return count } console.log(solution([1,1,2,3,4,5], 1)) console.log(solution([0,2,3,4], 1)) array 에 n 이 몇개 있는지 구하는 코드이다. 변수 count를 0으로 할당해놓고 반복문을 사용해 array.length 미만으로 반복을 돌게한다. 이때 반복문 안에 조건문을 쓰는데 만약 array[i]번째가 n 과 같다면 count 에 1을 더하게 하여 array 내에 n이 있을때마다 카운트가 올라가게 했다. function soluti.. 2023. 3. 17.