📌  相关文章
📜  在不使用任何多余空间的情况下检查数字是否是回文

📅  最后修改于: 2021-04-29 13:13:09             🧑  作者: Mango

给定数字“ n”,我们的目标是在不使用回文的情况下找出回文
任何多余的空间。我们无法复制数字。

例子:

Input  : 2332
Output : Yes it is Palindrome.
Explanation:
original number = 2332
reversed number = 2332
Both are same hence the number is palindrome.

Input :1111
Output :Yes it is Palindrome.

Input : 1234
Output : No not Palindrome.

递归解决方案将在下面的文章中讨论。
检查数字是否是回文

在这篇文章中,讨论了不同的解决方案。
1)我们可以比较第一位数字和最后一位数字,然后重复该过程。
2)对于第一个数字,我们需要数字的顺序。假设12321。将其除以10000将得到前导1。可以通过将mod取为10来检索尾随的1。
3)现在,将其减少到232。

(12321 % 10000)/10 = (2321)/10 = 232 

4)现在,需要将10000减少100倍。
这是上述算法的实现:

C++
// C++ program to find number is palindrome
// or not without using any extra space
#include 
using namespace std;
bool isPalindrome(int);
  
bool isPalindrome(int n)
{   
    // Find the appropriate divisor
    // to extract the leading digit
    int divisor = 1;
    while (n / divisor >= 10)
        divisor *= 10;
  
    while (n != 0)
    {
        int leading = n / divisor; 
        int trailing = n % 10;
  
        // If first and last digit 
        // not same return false
        if (leading != trailing)  
            return false;
  
        // Removing the leading and trailing
        // digit from number
        n = (n % divisor) / 10;
  
        // Reducing divisor by a factor 
        // of 2 as 2 digits are dropped
        divisor = divisor / 100;
    }
    return true;
}
  
// Driver code
int main()
{
    isPalindrome(1001) ? cout << "Yes, it is Palindrome" :
    cout << "No, not Palindrome";
    return 0;
}


Java
// Java program to find number is palindrome
// or not without using any extra space
public class GFG 
{      
    static boolean isPalindrome(int n)
    {   
        // Find the appropriate divisor
        // to extract the leading digit
        int divisor = 1;
        while (n / divisor >= 10)
            divisor *= 10;
       
        while (n != 0)
        {
            int leading = n / divisor; 
            int trailing = n % 10;
       
            // If first and last digit 
            // not same return false
            if (leading != trailing)  
                return false;
       
            // Removing the leading and trailing
            // digit from number
            n = (n % divisor) / 10;
       
            // Reducing divisor by a factor 
            // of 2 as 2 digits are dropped
            divisor = divisor / 100;
        }
        return true;
    }
       
    // Driver code
    public static void main(String args[])
    {
        if(isPalindrome(1001))
            System.out.println("Yes, it is Palindrome");
        else
            System.out.println("No, not Palindrome");
    } 
}
// This code is contributed by Sumit Ghosh


Python3
# Python program to find number 
# is palindrome or not without 
# using any extra space
  
# Function to check if given number 
# is palindrome or not without 
# using the extra space 
def isPalindrome(n):
  
    # Find the appropriate divisor
    # to extract the leading digit
    divisor = 1
    while (n / divisor >= 10):
        divisor *= 10
  
    while (n != 0):
          
        leading = n // divisor 
        trailing = n % 10
          
        # If first and last digit 
        # not same return false
        if (leading != trailing): 
            return False
          
        # Removing the leading and 
        # trailing digit from number
        n = (n % divisor)//10
          
        # Reducing divisor by a factor 
        # of 2 as 2 digits are dropped
        divisor = divisor/100
          
    return True
  
# Driver code
if(isPalindrome(1001)):
    print('Yes, it is palindrome')
else:
    print('No, not palindrome')
  
# This code is contributed by Danish Raza


C#
// C# program to find number 
// is palindrome or not without
// using any extra space
using System;
  
class GFG
{
    static bool isPalindrome(int n)
    { 
        // Find the appropriate 
        // divisor to extract 
        // the leading digit
        int divisor = 1;
        while (n / divisor >= 10)
            divisor *= 10;
      
        while (n != 0)
        {
            int leading = n / divisor; 
            int trailing = n % 10;
      
            // If first and last digit 
            // not same return false
            if (leading != trailing) 
                return false;
      
            // Removing the leading and 
            // trailing digit from number
            n = (n % divisor) / 10;
      
            // Reducing divisor by 
            // a factor of 2 as 2 
            // digits are dropped
            divisor = divisor / 100;
        }
        return true;
    }
      
    // Driver code
    static public void Main ()
    {
        if(isPalindrome(1001))
            Console.WriteLine("Yes, it " + 
                         "is Palindrome");
        else
            Console.WriteLine("No, not " + 
                            "Palindrome");
    }
}
  
// This code is contributed by m_kit


PHP
= 10)
        $divisor *= 10;
  
    while ($n != 0)
    {
        $leading = floor($n / $divisor); 
        $trailing = $n % 10;
  
        // If first and last digit 
        // not same return false
        if ($leading != $trailing) 
            return false;
  
        // Removing the leading and
        // trailing digit from number
        $n = ($n % $divisor) / 10;
  
        // Reducing divisor by a 
        // factor of 2 as 2 digits 
        // are dropped
        $divisor = $divisor / 100;
    }
    return true;
}
  
// Driver code
if(isPalindrome(1001) == true)
echo "Yes, it is Palindrome" ;
else
  
echo "No, not Palindrome";
  
// This code is contributed by ajit
?>


输出:

Yes, it is Palindrome