**
判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
**
示例 1:
输入: 121
输出: true
示例 2:
输入: -121
输出: false
解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
示例 3:
输入: 10
输出: false
解释: 从右向左读, 为 01 。因此它不是一个回文数。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/palindrome-number
1.力扣官方C#解答,用时最少:
复杂度分析
时间复杂度:O(\log n)O(logn),对于每次迭代,我们会将输入除以 1010,因此时间复杂度为 O(\log n)O(logn)。
空间复杂度:O(1)O(1)。我们只需要常数空间存放若干变量。
public class Solution {
public bool IsPalindrome(int x) {
// 特殊情况:
// 如上所述,当 x < 0 时,x 不是回文数。
// 同样地,如果数字的最后一位是 0,为了使该数字为回文,
// 则其第一位数字也应该是 0
// 只有 0 满足这一属性
if (x < 0 || (x % 10 == 0 && x != 0)) {
return false;
}
int revertedNumber = 0;
while (x > revertedNumber) {
revertedNumber = revertedNumber * 10 + x % 10;
x /= 10;
}
// 当数字长度为奇数时,我们可以通过 revertedNumber/10 去除处于中位的数字。
// 例如,当输入为 12321 时,在 while 循环的末尾我们可以得到 x = 12,revertedNumber = 123,
// 由于处于中位的数字不影响回文(它总是与自己相等),所以我们可以简单地将其去除。
return x == revertedNumber || x == revertedNumber / 10;
}
}
2.利用系统函数 (LINQ)
static void Main(string[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(IsPalindrome(n));
Console.ReadKey();
}
static bool IsPalindrome(int x)
{
string st = x.ToString();
string newStr = new string(x.ToString().Reverse().ToArray());
return st.Equals(newStr);
3.比较数字左右的值
static void Main(string[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(IsPalindrome1(n));
Console.ReadKey();
}
static bool IsPalindrome1(int x)
{
string str = x.ToString();
bool result = true;
for (int i = 0; i < str.Length; i++)
{
if (str[i]!=str[str.Length-1-i])
{
result = false;
break;
}
}
return result;
}
4.翻转数字,判断新旧数字是否相等(1)
static void Main(string[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(IsPalindrome1(n));
Console.ReadKey();
}
static bool IsPalindrome1(int x)
{
if (x < 0) return false;
var old = x;
Int64 result = 0;
do
{
var r = x % 10;
result = result * 10 + r;
x = x / 10;
} while (x!=0);
return old == result;
}
5.翻转数字,判断新旧数字是否相等(2)
static void Main(string[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(IsPalindrome1(n));
Console.ReadKey();
}
static bool IsPalindrome1(int x)
{
if (x < 0) return false;
if (x < 10) return true;
if (x % 10 == 0) return false;
int y = 0;
while (x>y)
{
int pop = x % 10;
x = x / 10;
y = y * 10 + pop;
}
return (x == y) || (x == y / 10);
}
6.转换成字符串进行前后比较(1)
static void Main(string[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(IsPalindrome1(n));
Console.ReadKey();
}
static bool IsPalindrome1(int x)
{
string y = x.ToString();
int len = y.Length;
string z = null;
for (int i = len-1; i >=0; i--)
{
z = z + y[i];
}
if (string.Equals(y, z))
return true;
else
return false;
}
7.转换成字符串进行前后比较(2)
static void Main(string[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(IsPalindrome1(n));
Console.ReadKey();
}
static bool IsPalindrome1(int x)
{
string str = x.ToString();
var chars = str.ToCharArray();
Array.Reverse(chars);
string revStr = new string(chars);
if (str == revStr)
return true;
else
return false;
}