| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 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 class PalindromeNumberByReversingDigits : IPalindromeNumber |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(d), where d is the number of digits in the integer x |
| | 19 | | /// Space complexity - O(1) |
| | 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 reversedHalf = 0; |
| | 39 | |
|
| 36 | 40 | | while (x > reversedHalf) |
| 26 | 41 | | { |
| 26 | 42 | | reversedHalf = (reversedHalf * 10) + (x % 10); |
| | 43 | |
|
| 26 | 44 | | x /= 10; |
| 26 | 45 | | } |
| | 46 | |
|
| 10 | 47 | | return x == reversedHalf || x == reversedHalf / 10; |
| 17 | 48 | | } |
| | 49 | | } |