반응형
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 |
Tags
- HTTP 완벽 가이드
- 크롤링
- 프로그래머스
- 프로그래머스 레벨 2
- socket
- 백준
- type challenge
- 타입 챌린지
- 그래프
- ip
- Node.js
- 수학
- 레벨 1
- 소켓
- dp
- 문자열
- BFS
- javascript
- 타입스크립트
- typescript
- 알고리즘
- Nestjs
- Algorithm
- 쉬운 문제
- HTTP
- 가천대
- 자바스크립트
- TCP
- Crawling
- dfs
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 |