LeetCode - The World's Leading Online Programming Learning Platform
input: 숫자 x
output: 주어진 숫자 x가 팰린드롬인지 Boolean 리턴
• -2^31 <= x <= 2^31 - 1
Follow up: Could you solve it without converting the integer to a string?
문자로 변환 후, 양 끝에서 부터 비교 ⭕
class Solution {
fun isPalindrome(x: Int): Boolean {
val num = x.toString()
var left = 0
var right = num.length-1
// 양끝에서 투 포인터 탐색
while (num[left] == num[right] && left < right) {
left++
right--
}
return (left >= right)
}
}
시간 복잡도: 전체 순회 = n
문자로 변환하지 않고 풀이가 가능하지만, 의미가 없는 듯?
→ 자릿수를 구해야하거나(or % 10)
→ 한자리씩 배열이나, 스택에 담아줘야 하거나