문제
Given an integer x, return true if x is a palindrome, and false otherwise.
Example 1:
Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.
Example 2:
Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Constraints:
- 231 <= x <= 231 - 1
Follow up:
Could you solve it without converting the integer to a string?
풀이
나의 풀이법
풀이 접근 과정
그냥 간단한 팰린드롬 문제였다.
앞과 대칭되는 뒤에 문자를 비교해서 틀리면 false를 리턴시키면 된다.
최종 소스코드
class Solution {
fun isPalindrome(x: Int): Boolean {
val s = x.toString()
val len = s.length
for (i in 0 until len/2) {
if (s[i] != s[len-i-1]) return false
}
return true
}
}
'LeetCode > Kotlin | Easy' 카테고리의 다른 글
[LeetCode/Kotlin]Easy - 217. Contains Duplicate (0) | 2023.06.10 |
---|---|
[LeetCode/Kotlin]Easy - 66. Plus One (0) | 2023.06.10 |
[LeetCode/Kotlin]Easy - 290. Word Pattern (0) | 2023.06.05 |
[LeetCode/Kotlin]Easy - 1470. Shuffle the Array (0) | 2023.06.05 |
[LeetCode/Kotlin]Easy - 14. Longest Common Prefix (0) | 2023.05.29 |