9. Palindrome Number

This commit is contained in:
Пытков Роман
2024-01-31 01:05:39 +03:00
parent 9d50f345d1
commit d15aa071c8
5 changed files with 73 additions and 5 deletions

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>_9._Palindrome_Number</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,6 @@
// See https://aka.ms/new-console-template for more information
using _9._Palindrome_Number;
var sol = new Solution();
Console.WriteLine(sol.IsPalindrome(0));

View File

@@ -0,0 +1,25 @@
namespace _9._Palindrome_Number;
public class Solution
{
public bool IsPalindrome(int x)
{
if (x < 0)
return false;
if (x < 10)
return true;
List<byte> digits = new List<byte>(32);
while (x != 0)
{
digits.Add((byte)(x%10));
x /= 10;
}
int half = digits.Count / 2;
for (int i = 0; i < half; i++)
if (digits[i] != digits[digits.Count - 1 - i])
return false;
return true;
}
}