이전 코드
class Solution {
public int solution(int n, int[][] computers) {
int answer = 0;
boolean [] searchCheck = new boolean [n];
for (int i = 0 ; i < n ; i++){
if (!searchCheck[i]){
dfs(computers, searchCheck, i);
answer ++;
}
}
return answer;
}
public boolean[] dfs (int [][] computers, boolean [] searchCheck, int i){
searchCheck[i] = true;
for (int j = 0 ; j < computers.length ; j++){
if(i != j && computers[i][j] == 1 && !searchCheck[j]){
searchCheck = dfs(computers, searchCheck, j);
}
}
return searchCheck;
}
}
최근 코드
class Solution {
boolean [] isCheck;
public int solution(int n, int[][] computers) {
int answer = 0;
isCheck = new boolean[n];
for (int i = 0 ; i < n ; i++){
if (!isCheck[i]){
dfs(computers, i);
answer ++;
}
}
return answer;
}
public void dfs(int [][] computers, int i){
isCheck[i] = true;
for (int j = 0 ; j < computers.length ; j++){
if (i != j && computers[i][j] == 1 && !isCheck[j]){
dfs(computers, j);
}
}
}
}
'프로그래머스 - JAVA' 카테고리의 다른 글
코딩테스트 연습 > 깊이/너비 우선 탐색(DFS/BFS) > 여행경로 (0) | 2021.06.17 |
---|---|
코딩테스트 연습 > 깊이/너비 우선 탐색(DFS/BFS) > 단어 변환 (0) | 2021.06.16 |
코딩테스트 연습 > 힙(Heap) > 디스크 컨트롤러 (0) | 2021.06.16 |
코딩테스트 연습 > 깊이/너비 우선 탐색(DFS/BFS) > 타겟 넘버 (0) | 2021.06.14 |
코딩테스트 연습 > 탐욕법(Greedy) > 구명보트 (0) | 2021.06.13 |