반응형
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 |
31 |
Tags
- typescript
- 크롤링
- dp
- ip
- Algorithm
- BFS
- 자바스크립트
- 프로그래머스
- 문자열
- 프로그래머스 레벨 2
- 백준
- javascript
- TCP
- 그래프
- 레벨 1
- Crawling
- Nestjs
- 알고리즘
- 수학
- HTTP
- 타입스크립트
- 소켓
- HTTP 완벽 가이드
- Node.js
- type challenge
- dfs
- 타입 챌린지
- 쉬운 문제
- socket
- 가천대
Archives
- Today
- Total
kakasoo
[node.js] 소수찾기 ( 프로그래머스 레벨2 ) 본문
반응형
// 프로그래머스 레벨2 소수찾기를 풀었습니다.
let visited = [];
let num = [];
let res = [];
const dfs = (curIdx, curValue = "") => {
visited[curIdx] = true;
curValue += num[curIdx];
if (!res.includes(curValue)) {
res.push(curValue);
}
for (let i = 0; i < num.length; i++) {
if (visited[i] === false) {
dfs(i, curValue);
visited[i] = false;
}
}
};
function solution(numbers) {
num = numbers.split("");
visited = new Array(numbers.length).fill(false);
for (let i = 0; i < numbers.length; i++) {
if (visited[i] === false) {
dfs(i);
visited[i] = false;
}
}
res = [...new Set(res.map((e) => Number(e)))].filter((e) =>
e === 0 || e === 1 ? false : true
);
let count = 0;
for (let i = 0; i < res.length; i++) {
let flag = true;
for (let j = 2; j <= res[i] / 2; j++) {
if (res[i] % j === 0) {
flag = false;
break;
}
}
if (flag) count++;
}
return count;
}
반응형
'프로그래밍 > 알고리즘 풀이' 카테고리의 다른 글
[node.js] 카펫 ( 프로그래머스 레벨2 ) (0) | 2021.06.29 |
---|---|
[node.js] 타겟 넘버 ( 프로그래머스 레벨2 ) (0) | 2021.06.29 |
[node.js] 가장 큰 수 ( 프로그래머스 레벨2 ) (0) | 2021.06.28 |
[node.js] 올바른 괄호 ( 프로그래머스 레벨2 ) (0) | 2021.06.28 |
[node.js] 124나라 ( 프로그래머스 레벨2 ) (0) | 2021.06.28 |