๐Ÿฅž 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 ํด๋ž˜์Šค๋ฅผ ์–ด๋–ป๊ฒŒ ์ž˜ ํ™œ์šฉํ•˜๋Š”์ง€๊ฐ€ ํฌ์ธํŠธ์˜€๋˜ ๊ฒƒ ๊ฐ™๋‹ค.