| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | | 3 | | // All Rights Reserved. |
| | | 4 | | // -------------------------------------------------------------------------------- |
| | | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | | 9 | | // known as Yevhenii Yeriemeieiv). |
| | | 10 | | // -------------------------------------------------------------------------------- |
| | | 11 | | |
| | | 12 | | namespace LeetCode.Algorithms.PalindromeNumber; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class PalindromeNumberByConvertingToString : IPalindromeNumber |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(d), where d is the number of digits in the integer x |
| | | 19 | | /// Space complexity - O(d), where d is the number of digits in the integer x |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="x"></param> |
| | | 22 | | /// <returns></returns> |
| | | 23 | | public bool IsPalindrome(int x) |
| | 17 | 24 | | { |
| | 17 | 25 | | switch (x) |
| | | 26 | | { |
| | | 27 | | case < 0: |
| | 2 | 28 | | return false; |
| | | 29 | | case < 10: |
| | 3 | 30 | | return true; |
| | | 31 | | } |
| | | 32 | | |
| | 12 | 33 | | if (x % 10 == 0) |
| | 2 | 34 | | { |
| | 2 | 35 | | return false; |
| | | 36 | | } |
| | | 37 | | |
| | 10 | 38 | | var xString = x.ToString(); |
| | 10 | 39 | | var i = 0; |
| | 10 | 40 | | var j = xString.Length - 1; |
| | | 41 | | |
| | 29 | 42 | | while (i < j) |
| | 20 | 43 | | { |
| | 20 | 44 | | if (xString[i].Equals(xString[j])) |
| | 19 | 45 | | { |
| | 19 | 46 | | i++; |
| | 19 | 47 | | j--; |
| | 19 | 48 | | } |
| | | 49 | | else |
| | 1 | 50 | | { |
| | 1 | 51 | | return false; |
| | | 52 | | } |
| | 19 | 53 | | } |
| | | 54 | | |
| | 9 | 55 | | return true; |
| | 17 | 56 | | } |
| | | 57 | | } |