반응형
    
    
    
  
                              Notice
                              
                          
                        
                          
                          
                            Recent Posts
                            
                        
                          
                          
                            Recent Comments
                            
                        
                          
                          
                            Link
                            
                        
                    | 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 | 
| 9 | 10 | 11 | 12 | 13 | 14 | 15 | 
| 16 | 17 | 18 | 19 | 20 | 21 | 22 | 
| 23 | 24 | 25 | 26 | 27 | 28 | 29 | 
| 30 | 
                            Tags
                            
                        
                          
                          - TCP
- 알고리즘
- type challenge
- 백준
- Node.js
- dp
- 소켓
- typescript
- HTTP 완벽 가이드
- 크롤링
- 쉬운 문제
- 그래프
- Crawling
- 프로그래머스 레벨 2
- BFS
- socket
- 가천대
- 타입 챌린지
- dfs
- ip
- Nestjs
- HTTP
- 자바스크립트
- javascript
- 수학
- 레벨 1
- Algorithm
- 타입스크립트
- 문자열
- 프로그래머스
                            Archives
                            
                        
                          
                          - Today
- Total
kakasoo
[node.js] 튜플 ( 프로그래머스 레벨2 ) 본문
반응형
    
    
    
  // 프로그래머스 레벨2 튜플을 풀었습니다.
const solution = (s) => {
    // "{{4,2,3},{3},{2,3,4,1},{2,3}}"
    const arr = s
        .split("},")
        .map((str) => {
            // [ '4,2,3', '3', '2,3,4,1', '2,3' ]
            return str
                .split("")
                .filter((el) => el !== "{" && el !== "}")
                .join("");
        })
        .sort((a, b) => {
            // [ '3', '2,3', '4,2,3', '2,3,4,1' ]
            return a.length - b.length;
        })
        .map((el) => {
            // [ '3' ], [ '2', '3' ], [ '4', '2', '3' ], [ '2', '3', '4', '1' ]
            return el.split(",");
        });
    const answer = [];
    for (let i = 0; i < arr.length; i++) {
        const curNumber = arr[i].join("");
        answer.push(curNumber);
        for (let j = i; j < arr.length; j++) {
            arr[j].splice(arr[j].indexOf(curNumber), 1);
        }
    }
    return answer.map((el) => Number(el));
};
일단 arr를 위 주석과 같이 차례대로 분리해나가고, 숫자를 뽑는 과정, 두 단계로 처리했다.
반응형
    
    
    
  '프로그래밍 > 알고리즘 풀이' 카테고리의 다른 글
| [node.js] 괄호 회전하기 ( 프로그래머스 레벨2 ) (0) | 2021.07.01 | 
|---|---|
| [node.js] 쿼드 압축 후 개수 세기 ( 프로그래머스 레벨2 ) (0) | 2021.07.01 | 
| [node.js] 땅따먹기 ( 프로그래머스 레벨2 ) (0) | 2021.07.01 | 
| [node.js] 2개 이하로 다른 비트 ( 프로그래머스 레벨2 ) (0) | 2021.07.01 | 
| [node.js] 이진 변환 반복하기 ( 프로그래머스 레벨2 ) (0) | 2021.07.01 | 
 
                   
                   
                  