πŸ₯ž BE
home

125_Valid Palindrome

λ‹΄λ‹Ήμž
μ™„λ£Œ μ—¬λΆ€
Solved
μš”μ•½
λ‚ μ§œ
2024/06/25
νƒœκ·Έ
κ΅¬ν˜„
λ¬Έμžμ—΄
λ‚œμ΄λ„
Easy
좜처
LeetCode

처음 μ½”λ“œ

class Solution { public boolean isPalindrome(String s) { int len = s.length(); for (int i = 0; i < (len/2); i++) { if (s.charAt(i) != s.charAt(len-1-i)) { return false; } } return true; } }
Java
볡사
일반적인 μ†Œλ¬Έμžλ§Œ μžˆλŠ” κ²½μš°μ—λŠ” μœ„μ˜ μ½”λ“œκ°€ λ§žκ² μ§€λ§Œ, νŠΉμˆ˜λ¬Έμžμ™€ λŒ€μ†Œλ¬Έμžλ₯Ό μƒκ°ν•˜μ§€ λͺ»ν–ˆλ‹€.
μžλ°”μ˜ Character 클래슀λ₯Ό 잘 ν™œμš©ν•΄λ³΄μž.
Character 클래슀의 .isLetterOrDigitλŠ” ν•΄λ‹Ή λ¬Έμžκ°€ 문자(μ•ŒνŒŒλ²³) λ˜λŠ” μˆ«μžμΈμ§€ μ—¬λΆ€λ₯Ό ν™•μΈν•˜λŠ” 데 μ‚¬μš©λœλ‹€.
.toLowerCaseλŠ” ν•΄λ‹Ή 문자λ₯Ό μ†Œλ¬Έμžλ‘œ λ³€κ²½ν•˜λŠ” 데 μ‚¬μš©λœλ‹€. λŒ€μ†Œλ¬Έμžλ₯Ό κ΅¬λΆ„ν•˜μ§€ μ•ŠλŠ” 경우 μ‚¬μš©ν•˜λ©΄ 될 것 κ°™λ‹€.

μˆ˜μ • μ½”λ“œ

class Solution{ public boolean isPalindrome(String s) { int left = 0; int right = s.length() -1; while (left < right) { if (!Character.isLetterOrDigit(s.charAt(left))) { left++; } else if (!Character.isLetterOrDigit(s.charAt(right))) { right--; } else { if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) { return false; } left++; right--; } } return true; } }
Java
볡사

문제 ν•΄κ²° 아이디어

Java Character 클래슀λ₯Ό μ–΄λ–»κ²Œ 잘 ν™œμš©ν•˜λŠ”μ§€κ°€ ν¬μΈνŠΈμ˜€λ˜ 것 κ°™λ‹€.