
Problem
Solution
Submissions
Check Palindrome Number
Certification: Basic Level
Accuracy: 0%
Submissions: 0
Points: 5
Write a JavaScript program to determine if a given integer is a palindrome number. A palindrome number reads the same backward as forward. You should solve this without converting the integer to a string.
Example 1
- Input: num = 121
- Output: true
- Explanation:
- The number 121 reads the same from left to right and right to left.
- When we reverse 121, we get 121.
- Since the original number equals its reverse, it is a palindrome.
- Therefore, the output is true.
- The number 121 reads the same from left to right and right to left.
Example 2
- Input: num = -121
- Output: false
- Explanation:
- The number -121 has a negative sign at the beginning.
- When reversed, it would be 121- which is not a valid number format.
- Negative numbers cannot be palindromes due to the negative sign.
- Therefore, the output is false.
- The number -121 has a negative sign at the beginning.
Constraints
- -2^31 ≤ num ≤ 2^31 - 1
- You cannot convert the integer to a string
- Negative numbers should return false
- Time Complexity: O(log n) where n is the input number
- Space Complexity: O(1)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Handle negative numbers first - they cannot be palindromes
- Use mathematical operations to reverse the number digit by digit
- Extract the last digit using the modulo operator (num % 10)
- Remove the last digit by integer division (Math.floor(num / 10))
- Build the reversed number by multiplying the current reversed number by 10 and adding the extracted digit
- Continue this process until the original number becomes 0
- Compare the original number with the reversed number to determine if it's a palindrome