[LeetCode] 125. Valid Palindrome (팰린드롬)

🔗 문제

https://leetcode.com/problems/valid-palindrome/

 

Valid Palindrome - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

📝 포인트

char[]로 변환해서 문자, 숫자 검사 후, List에 담았다

Character.isLetterOrDigit

 

💻 코드

class Solution {
    public boolean isPalindrome(String s) {
        
        boolean answer = true;

        if(s == null){
            return true;
        }

        List<String> list = new ArrayList<>();
        char[] cc = s.toLowerCase().toCharArray();


        for (int i = 0; i < cc.length; i++) {
            if(Character.isLetterOrDigit(cc[i])) {
                list.add(String.valueOf(cc[i]));
            }
        }

        int startIndex = 0;
        int lastIndex = list.size()-1;
        //System.out.println("list.size = " + list.size());
        //System.out.println("lastIndex = " + lastIndex);

        for(int i=0; i < list.size(); i++){
            String x = list.get(i);
            String y = list.get(lastIndex);

            //System.out.println(x + " :: " + y);

            if(x.equals(y)) {

            }else {
                answer = false;
                break;
            }

            lastIndex--;
        }


        System.out.println("answer = " + answer);

        return answer;
        
    }
}
 

댓글

Designed by JB FACTORY