반응형
Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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
more
Archives
Today
Total
관리 메뉴

배움과 기록의 장

[백준] 10988번: 팰린드롬인지 확인하기 (Java) 본문

problem solving

[백준] 10988번: 팰린드롬인지 확인하기 (Java)

chaeunii 2023. 3. 9. 17:51

✅ 문제

https://www.acmicpc.net/problem/10988

 

✅ 풀이

  • 주어진 단어 맨 처음을 head, 맨 끝을 tail로 정하여 문자를 비교한다. 같으면 isPalindrome 초기값 1 그대로 두고, 다음 문자 비교를 위해 head++ tail-- 후 반복문 계속 진행, 다르면 isPalindrome 0 할당 후 반복문을 종료(break)한다.
  • 반복문은 break 안걸리면 head < tail 일때까지 진행된다. 
  •  

 

✅ 코드

import java.util.Scanner;

public class Main{
    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();


        int head = 0;
        int tail = str.length()-1;

        int isPalindrome = 1;
        while(head < tail){
            if (str.charAt(head) != str.charAt(tail)) {
                isPalindrome = 0;
                break;
            }
            head++;
            tail--;
        }
        System.out.println(isPalindrome);
    }
}

 

 

반응형