Tensorum
YouTubeనేర్చుకోవడం ప్రారంభించండి
LeetCode 9 · Other

Palindrome Number

LeetCode 9 · సులభం · పరిష్కారం, intuition & దశలవారీ వాక్‌త్రూ

Reverse half the digits and compare.

సులభంMath
LeetCode లో తెరవండి ↗

నేను Palindrome Number (LeetCode 9) ను ఇలా పరిష్కరించాను — Other ప్యాటర్న్‌లోని ఒక సులభం ప్రాబ్లమ్. కింద: intuition, దశలవారీ approach, నా accepted Python కోడ్, animated test cases, line-by-line debugger.

కింది వివరణ ఇంగ్లీష్‌లో ఉంది.

Intuition

Reverse only the second half of the number and compare it with the first half. Stop when the reversed part is no longer smaller than what's left; for odd lengths, drop the middle digit with // 10.

Approach

  1. 1

    Negatives and numbers ending in 0 (except 0) aren't palindromes.

  2. 2

    Move digits from x onto reversed until reversed ≥ x.

  3. 3

    Compare x with reversed or reversed // 10.

నేను ఎలా పరిష్కరించాను

Half reversalPythonO(log x) time
class Solution:
    def isPalindrome(self, x: int) -> bool:
        if x<0 or (x!=0 and x%10==0):
            return False
        #121
        #reverse from right till middle is same return true
        reversed = 0
        while x>reversed:
            reversed = reversed*10+x%10
            x = x//10
        return True if reversed == x or reversed//10 == x else False
        #12321 12, 12, for odd length revesrsed will be 123, so if we devide 123/10 we will get 12
స్టెప్-త్రూ లోడ్ అవుతోంది…

Palindrome Number — తరచూ అడిగే ప్రశ్నలు

Palindrome Number వెనుక ఉన్న intuition ఏమిటి?

Reverse only the second half of the number and compare it with the first half. Stop when the reversed part is no longer smaller than what's left; for odd lengths, drop the middle digit with // 10.

Palindrome Number (LeetCode 9) ను ఎలా పరిష్కరించాలి?

1. Negatives and numbers ending in 0 (except 0) aren't palindromes. 2. Move digits from x onto reversed until reversed ≥ x. 3. Compare x with reversed or reversed // 10.

Palindrome Number యొక్క time, space complexity ఎంత?

Time: O(log x). Space: O(1). (Accepted approach: Half reversal.)

Palindrome Number లో సాధారణ పొరపాట్లు ఏమిటి?

Converting to a string works but isn't the intended trick.