-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution9.cs
37 lines (32 loc) · 931 Bytes
/
Solution9.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
namespace LeetCode.Solutions;
public class Solution9
{
/// <summary>
/// Given an integer x, return true if x is palindrome integer.
/// An integer is a palindrome when it reads the same backward as forward.
/// For example, 121 is a palindrome while 123 is not.
/// <a href="https://leetcode.com/problems/palindrome-number/">See the problem</a>
/// </summary>
/// <remarks>Time complexity O(n)</remarks>
public bool IsPalindrome(int x)
{
if (x < 0)
return false;
var str = x.ToString();
var len = str.Length;
var isPalindrome = true;
if (len == 1)
{
return isPalindrome;
}
for (var i = 0; i < len / 2; i++)
{
if (str[i] != str[len - i - 1])
{
isPalindrome = false;
break;
}
}
return isPalindrome;
}
}