매일 Algorithm

신재원·2023년 5월 28일
1

Algorithm

목록 보기
132/243

프로그래머스 (배열 조각하기)

import java.util.Arrays;
public class problem440 {
    class Solution {
        public int[] solution(int[] arr, int[] query) {

            // Arrays.copyOfRange() : 배열 자르기
            for (int i = 0; i < query.length; i++) {
                if (i % 2 == 0) {
                    arr = Arrays.copyOfRange(arr, 0, query[i] + 1);
                } else {
                    arr = Arrays.copyOfRange(arr, query[i], arr.length);
                }
            }
            return arr;
        }
    }
}

프로그래머스 (세 개의 구분자)

import java.util.ArrayList;
import java.util.List;
public class problem441 {
    class Solution {
        public String[] solution(String myStr) {
            // a,b,c를 0으로 replace
            String replace = myStr.replaceAll("[a,b,c]", "0");

            // 0을 기준으로 split
            String[] str = replace.split("0");
            List<String> list = new ArrayList<>();

            for (int i = 0; i < str.length; i++) {
                if (!str[i].equals("")) {
                    list.add(str[i]);
                }
            }
            // 빈배열 반환
            if (list.isEmpty()) {
                return new String[]{"EMPTY"};
            }

            String[] answer = new String[list.size()];
            for (int i = 0; i < answer.length; i++) {
                answer[i] = list.get(i);
            }

            return answer;
        }
    }

}

백준 1260번 (DFS와 BFS)

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
    static int[][] graph;
    static boolean[] visit;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int n = in.nextInt();
        int m = in.nextInt();
        int v = in.nextInt();

        // 배열 생성
        graph = new int[n + 1][n + 1];

        for (int i = 0; i < m; i++) {
            int a = in.nextInt();
            int b = in.nextInt();

            // 양방향 관계
            graph[a][b] = 1;
            graph[b][a] = 1;
        }

        visit = new boolean[n + 1];
        DFS(v);
        System.out.println();

        visit = new boolean[n + 1];
        BFS(v);
    }

    static void DFS(int v) {
        visit[v] = true; // 방문 체크

        System.out.print(v + " ");

        // 탈출 조건
        if (v == graph.length) return;

        for (int i = 1; i < graph.length; i++) {
            // 노드가 연결되어있으면서, 방문하지 않은경우
            if (!visit[i] && graph[v][i] == 1) {
                DFS(i);
            }
        }
    }

    static void BFS(int V) {
        Queue<Integer> queue = new LinkedList<>();
        queue.add(V);
        visit[V] = true;

        while (!queue.isEmpty()) {
            int temp = queue.poll();
            System.out.print(temp + " ");

            for (int i = 1; i < graph.length; i++) {
                if (graph[temp][i] == 1 && !visit[i]) {
                    queue.add(i);
                    visit[i] = true;
                }
            }
        }
    }
}

0개의 댓글