
Problem
Solution
Submissions
Check if a Number is a Palindrome
Certification: Basic Level
Accuracy: 66.67%
Submissions: 3
Points: 5
Write a C# program to check if a given integer is a palindrome. A palindrome number reads the same backward as forward.
Example 1
- Input: x = 121
- Output: true
- Explanation:
- Step 1: Check if the number is negative. It's not, so continue.
- Step 2: Convert the integer to a string "121".
- Step 3: Compare characters from both ends of the string, moving inward.
- Step 4: All characters match, so the number is a palindrome.
Example 2
- Input: x = -121
- Output: false
- Explanation:
- Step 1: Check if the number is negative. It is, so it cannot be a palindrome due to the negative sign.
- Step 2: Return false immediately as negative numbers can't be palindromes.
Constraints
- The input is a 32-bit signed integer
- 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
- Consider edge cases like negative numbers and zero.
- You can convert the number to a string for easier comparison.
- Alternatively, you can reverse the number by constructing a new integer.
- Be careful about integer overflow when reversing.
- Compare the reversed number with the original.