9. Palindrome Number
Intuition
The intuition is to convert the number to a string and then check if it reads the same forwards and backwards. This is a straightforward approach that makes it easy to compare characters from both ends of the number.
Approach
- First, we handle the edge case where negative numbers cannot be palindromes
- Convert the number to a string using
strconv.Itoa()
- Convert the string to a rune slice to handle any potential Unicode characters
- Use two pointers (i and j) starting from both ends of the string
- Compare characters at both pointers and move them towards the center
- If any characters don't match, return false
- If all characters match, return true
Complexity
- Time complexity: O(n)
- Space complexity: O(n)
Keywords
- Two Pointers
- String Conversion
- Palindrome