목록Algorithm (10)
말랑한 하루
※ JavaScript const getCombinations = function (arr, select) { const results = []; if (select === 1) return arr.map((el) => [el]); arr.forEach((fixed, index, origin) => { const rest = origin.slice(index + 1); const combinations = getCombinations(rest, select - 1); const attached = combinations.map((el) => [fixed, ...el]); results.push(...attached); }); return results; }
[ 구현 순서 ] 0) Value (숫자)는 표현하고자하는 List (or String)에 바로 추가한다 1) 연산자에 가중치를 부여한다 ('+', '-' 동일 1) ('*', '/' 동일 2) 2) 모든 연산자('+', '-', '*', '/')는 스택에 담는다. 3) 연산자를 스택에 담기 전, 가중치에 따라 다음과같이 행동한다 3-1) 스택의 최상위 값(stack.top())의 가중치를 새로운 연산자와 비교한다 3-2) 만약 크거나 같다면, 스택의 최상위값 빼서 List (or String)에 담는다. 4) 여는 괄호 ( '(', '[', '{' )는 스택에 담는다. 5) 닫는 괄호 ( ')'. ']', '}' )가 나오는순간, 스택에서 여는괄호가 나올때까지 모든 연산자를 List (or String..