백준 팰린드롬? - 10942 [JAVA] - 23년 4월 15일

Denia·2023년 4월 15일
0

코딩테스트 준비

목록 보기
179/201
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) throws IOException {
        BjSolution sol = new BjSolution();

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        br.readLine();
        String nums = br.readLine();
        int quizCount = Integer.parseInt(br.readLine());
        String[] quizs = new String[quizCount];
        for (int i = 0; i < quizCount; i++) {
            quizs[i] = br.readLine();
        }

        sol.solution(nums, quizs);
    }
}

class BjSolution {

    private int[] arr;

    public void solution(String nums, String[] quizs) {
        arr = Arrays.stream(nums.split(" ")).mapToInt(Integer::parseInt).toArray();

        StringBuilder sb = new StringBuilder();

        for (String quiz : quizs) {
            sb.append(isPalindrome(quiz)).append("\n");
        }

        System.out.println(sb);
    }

    private int isPalindrome(String quiz) {
        String[] quizConditions = quiz.split(" ");
        int start = Integer.parseInt(quizConditions[0]) - 1;
        int end = Integer.parseInt(quizConditions[1]) - 1;

        int count = end - start + 1;
        int limit;

        //odd
        if (count % 2 == 1) {
            limit = count / 2;
        }
        //even
        else {
            limit = count / 2 + 1;
        }
        for (int i = 0; i < limit; i++) {
            if (arr[start + i] != arr[end - i]) {
                return 0;
            }
        }

        return 1;
    }
}

profile
HW -> FW -> Web

0개의 댓글