-
Notifications
You must be signed in to change notification settings - Fork 4
[JooKangsan] 25.01.09 #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| function solution(numbers) { | ||
| const nums = [ | ||
| "zero", | ||
| "one", | ||
| "two", | ||
| "three", | ||
| "four", | ||
| "five", | ||
| "six", | ||
| "seven", | ||
| "eight", | ||
| "nine", | ||
| ]; | ||
| let answer = numbers; | ||
|
|
||
| for (let i = 0; i < nums.length; i++) { | ||
| answer = answer.replace(nums[i], i); | ||
| } | ||
|
|
||
| return Number(answer); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| function solution(my_string) { | ||
| let arr = my_string.split(' '); | ||
| let result = Number(arr[0]); | ||
|
|
||
| for(let i = 1; i < arr.length; i += 2) { | ||
| const sign = arr[i]; | ||
| const num = Number(arr[i + 1]); | ||
|
|
||
| if(sign === '+') { | ||
| result += num; | ||
| } | ||
| if(sign === '-') { | ||
| result -= num; | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| function solution(s){ | ||
| const stack = []; | ||
|
|
||
| for (let i = 0 ; i < s.length ; i++) { | ||
| if(s[i] == '('){ | ||
| stack.push(')'); | ||
| }else if(s[i] == '['){ | ||
| stack.push(']'); | ||
| }else if(s[i] == '{'){ | ||
| stack.push('}'); | ||
| }else{ | ||
| if (s[i] !== stack.pop()) { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| return stack.length === 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| function solution(board, moves) { | ||
| let answer = 0; | ||
| let stack = []; | ||
|
|
||
| for(let move of moves) { | ||
| let col = move - 1; | ||
|
|
||
| for(let row = 0; row < board.length; row++) { | ||
| if(board[row][col] !== 0) { | ||
| let doll = board[row][col]; | ||
| board[row][col] = 0; | ||
| if(stack[stack.length - 1] === doll) { | ||
| stack.pop(); | ||
| answer += 2; | ||
| } else { | ||
| stack.push(doll); | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return answer; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| function solution(s) { | ||
| let stack = []; | ||
| let arr = s.split(" "); | ||
|
|
||
| for(let i = 0; i < arr.length; i++) { | ||
| if(arr[i] === "Z") { | ||
| stack.pop(); | ||
| } else { | ||
| stack.push(Number(arr[i])); | ||
| } | ||
| } | ||
|
|
||
| let sum = 0; | ||
| for(let i = 0; i < stack.length; i++) { | ||
| sum += stack[i]; | ||
| } | ||
|
|
||
| return sum; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| function solution(dartResult) { | ||
| let scores = []; | ||
| let currentNumber = ''; | ||
|
|
||
| for(let i = 0; i < dartResult.length; i++) { | ||
| let char = dartResult[i]; | ||
|
|
||
| if(!isNaN(char)) { | ||
| currentNumber += char; | ||
| } | ||
| else if(char === 'S' || char === 'D' || char === 'T') { | ||
| let score = Number(currentNumber); | ||
|
|
||
| if(char === 'D') score = Math.pow(score, 2); | ||
| if(char === 'T') score = Math.pow(score, 3); | ||
|
|
||
| scores.push(score); | ||
| currentNumber = ''; | ||
| } | ||
| else { | ||
| if(char === '*') { | ||
| scores[scores.length-1] *= 2; | ||
| if(scores.length > 1) { | ||
| scores[scores.length-2] *= 2; | ||
| } | ||
| } | ||
| else if(char === '#') { | ||
| scores[scores.length-1] *= -1; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return scores.reduce((acc, cur) => acc + cur, 0); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| function solution(my_string) { | ||
| return my_string.split('').reverse().join(''); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| # JavaScript Stack 알고리즘 | ||
|
|
||
| ## 1. 스택의 기본 구현 | ||
| ```javascript | ||
| class Stack { | ||
| constructor() { | ||
| this.items = []; | ||
| this.count = 0; | ||
| } | ||
|
|
||
| // 요소 추가 | ||
| push(element) { | ||
| this.items[this.count] = element; | ||
| this.count++; | ||
| return this.count - 1; | ||
| } | ||
|
|
||
| // 요소 제거 | ||
| pop() { | ||
| if(this.count === 0) return undefined; | ||
|
|
||
| this.count--; | ||
| const result = this.items[this.count]; | ||
| delete this.items[this.count]; | ||
| return result; | ||
| } | ||
|
|
||
| // 최상단 요소 확인 | ||
| peek() { | ||
| return this.items[this.count - 1]; | ||
| } | ||
|
|
||
| // 스택이 비어있는지 확인 | ||
| isEmpty() { | ||
| return this.count === 0; | ||
| } | ||
|
|
||
| // 스택 크기 반환 | ||
| size() { | ||
| return this.count; | ||
| } | ||
|
|
||
| // 스택 초기화 | ||
| clear() { | ||
| this.items = []; | ||
| this.count = 0; | ||
| return this; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## 2. 스택을 활용한 알고리즘 예시 | ||
|
|
||
| ### 2.1 괄호 매칭 확인 | ||
| ```javascript | ||
| function isValidParentheses(str) { | ||
| const stack = []; | ||
| const brackets = { | ||
| ')': '(', | ||
| '}': '{', | ||
| ']': '[' | ||
| }; | ||
|
|
||
| for (let char of str) { | ||
| if (!brackets[char]) { | ||
| stack.push(char); | ||
| } else { | ||
| if (stack.pop() !== brackets[char]) return false; | ||
| } | ||
| } | ||
|
|
||
| return stack.length === 0; | ||
| } | ||
| ``` | ||
|
|
||
| ### 2.2 후위 표기식 계산 | ||
| ```javascript | ||
| function evaluatePostfix(expr) { | ||
| const stack = []; | ||
|
|
||
| for(let token of expr.split(' ')) { | ||
| if(!isNaN(token)) { | ||
| stack.push(Number(token)); | ||
| } else { | ||
| const b = stack.pop(); | ||
| const a = stack.pop(); | ||
|
|
||
| switch(token) { | ||
| case '+': stack.push(a + b); break; | ||
| case '-': stack.push(a - b); break; | ||
| case '*': stack.push(a * b); break; | ||
| case '/': stack.push(a / b); break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return stack[0]; | ||
| } | ||
| ``` | ||
|
|
||
| ### 2.3 히스토리 관리 (실행 취소/재실행) | ||
| ```javascript | ||
| class UndoRedoStack { | ||
| constructor() { | ||
| this.undoStack = []; | ||
| this.redoStack = []; | ||
| } | ||
|
|
||
| // 작업 수행 | ||
| doAction(action) { | ||
| this.undoStack.push(action); | ||
| this.redoStack = []; // 새 작업 시 redo 스택 초기화 | ||
| } | ||
|
|
||
| // 실행 취소 | ||
| undo() { | ||
| if (this.undoStack.length === 0) return null; | ||
| const action = this.undoStack.pop(); | ||
| this.redoStack.push(action); | ||
| return action; | ||
| } | ||
|
|
||
| // 재실행 | ||
| redo() { | ||
| if (this.redoStack.length === 0) return null; | ||
| const action = this.redoStack.pop(); | ||
| this.undoStack.push(action); | ||
| return action; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## 3. 시간 복잡도 | ||
|
|
||
| ### 기본 연산 | ||
| - Push: O(1) | ||
| - Pop: O(1) | ||
| - Peek: O(1) | ||
| - isEmpty: O(1) | ||
| - Size: O(1) | ||
|
|
||
| ### 메모리 사용 | ||
| - 공간 복잡도: O(n), n은 스택에 저장된 요소의 수 | ||
|
|
||
| ## 4. 활용 사례 | ||
|
|
||
| ### 4.1 함수 호출 스택 | ||
| ```javascript | ||
| function first() { | ||
| second(); | ||
| console.log('first'); | ||
| } | ||
|
|
||
| function second() { | ||
| third(); | ||
| console.log('second'); | ||
| } | ||
|
|
||
| function third() { | ||
| console.log('third'); | ||
| } | ||
|
|
||
| first(); | ||
| // 출력: | ||
| // third | ||
| // second | ||
| // first | ||
| ``` | ||
|
|
||
| ### 4.2 웹 브라우저 방문 기록 | ||
| ```javascript | ||
| class BrowserHistory { | ||
| constructor() { | ||
| this.history = []; | ||
| this.current = -1; | ||
| } | ||
|
|
||
| visit(url) { | ||
| this.current++; | ||
| this.history[this.current] = url; | ||
| this.history.length = this.current + 1; | ||
| } | ||
|
|
||
| back() { | ||
| if (this.current > 0) { | ||
| this.current--; | ||
| return this.history[this.current]; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| forward() { | ||
| if (this.current < this.history.length - 1) { | ||
| this.current++; | ||
| return this.history[this.current]; | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## 5. 주의사항 | ||
|
|
||
| 1. 메모리 관리 | ||
| - 스택 크기 제한 고려 | ||
| - 메모리 누수 방지를 위한 참조 해제 | ||
|
|
||
| 2. 에러 처리 | ||
| - 빈 스택에서의 pop 연산 처리 | ||
| - 스택 오버플로우 방지 | ||
|
|
||
| 3. 성능 최적화 | ||
| - 배열 크기 동적 조정 최소화 | ||
| - 불필요한 복사 연산 방지 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
문자열도 이터러블해서 스프레드 연산자로 쓰면 더 가독성 있을 것 같습니다!