문제링크
풀이1
class Solution {
fun isPalindrome(x: Int): Boolean {
if (x < 0) return false
val str = x.toString()
return str == str.reversed()
}
}
풀이2
class Solution {
fun isPalindrome(x: Int): Boolean {
if (x < 0) return false
var reversedInt = 0
var temp = x
while (temp > 0) {
reversedInt = reversedInt * 10 + temp % 10
temp = temp / 10
}
return x == reversedInt
}
}
풀이3
class Solution {
fun isPalindrome(x: Int): Boolean {
if (x < 0 || (x != 0 && x % 10 == 0)) return false
var reversedInt = 0
var temp = x
while (temp > reversedInt) {
reversedInt = reversedInt * 10 + temp % 10
temp = temp / 10
}
return (temp == reversedInt || temp == (reversedInt / 10))
}
}