在不使用任何额外空格的情况下检查数字是否为回文 |设置 2
给定一个数字“n”,我们的目标是在不使用任何额外空间的情况下找出它是否是回文。我们无法制作新的号码副本。
例子:
Input: n = 2332
Output: Yes it is Palindrome.
Explanation:
original number = 2332
reversed number = 2332
Both are same hence the number is palindrome.
Input: n=1111
Output: Yes it is Palindrome.
Input: n = 1234
Output: No not Palindrome.
其他方法:本文的 Set-1 中讨论了其他递归方法和比较数字的方法。在这里,我们正在讨论其他方法。
方法:这种方法取决于3个主要步骤,找到数字中的位数。将数字从中间分成两部分。注意长度为奇数的情况,在这种情况下,我们将不得不使用中间元素两次。检查两个号码中的号码是否相同。请按照以下步骤解决问题:
- 将变量K初始化为数字n 的长度。
- 将变量ans初始化为0。
- 使用变量i迭代范围[0, K/2)并执行以下任务:
- 将n%10的值放入变量ans中,然后将n除以10。
- 如果K%2等于1 ,则将n%10的值放入变量ans 中。
- 执行上述步骤后,如果ans等于n那么它是一个回文,否则不是。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find if the number is a
// pallindrome or not
bool isPalindrome(int n)
{
if (n < 0)
return false;
if (n < 10)
return true;
// Find the length of the number
int K = ceil(log(n) / log(10));
int ans = 0;
// Partition the number into 2 halves
for (int i = 0; i < K / 2; i++) {
ans = ans * 10 + n % 10;
n = n / 10;
}
if (K % 2 == 1)
ans = ans * 10 + n % 10;
// Equality Condition
return (ans == n);
}
// Driver Code
int main()
{
isPalindrome(1001) ? cout << "Yes, it is Palindrome"
: cout << "No, not Palindrome";
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find if the number is a
// pallindrome or not
static boolean isPalindrome(int n)
{
if (n < 0)
return false;
if (n < 10)
return true;
// Find the length of the number
int K = (int) Math.ceil(Math.log(n) / Math.log(10));
int ans = 0;
// Partition the number into 2 halves
for (int i = 0; i < K / 2; i++) {
ans = ans * 10 + n % 10;
n = n / 10;
}
if (K % 2 == 1)
ans = ans * 10 + n % 10;
// Equality Condition
return (ans == n);
}
// Driver Code
public static void main(String[] args)
{
System.out.print(isPalindrome(1001) ? "Yes, it is Palindrome"
: "No, not Palindrome");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python code for the above approach
import math as Math
# Function to find if the number is a
# pallindrome or not
def isPalindrome(n):
if (n < 0):
return False
if (n < 10):
return True
# Find the length of the number
K = Math.ceil(Math.log(n) / Math.log(10))
ans = 0
# Partition the number into 2 halves
for i in range(0, K // 2):
ans = ans * 10 + n % 10
n = Math.floor(n / 10)
if (K % 2 == 1):
ans = ans * 10 + n % 10
# Equality Condition
return (ans == n)
# Driver Code
print("Yes, it is Palindrome") if isPalindrome(
1001) else print("No, not Palindrome")
# This code is contributed by Saurabh jaiswal
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find if the number is a
// pallindrome or not
static bool isPalindrome(int n)
{
if (n < 0)
return false;
if (n < 10)
return true;
// Find the length of the number
int K = (int)Math.Ceiling(Math.Log(n) / Math.Log(10));
int ans = 0;
// Partition the number into 2 halves
for (int i = 0; i < K / 2; i++)
{
ans = ans * 10 + n % 10;
n = n / 10;
}
if (K % 2 == 1)
ans = ans * 10 + n % 10;
// Equality Condition
return (ans == n);
}
// Driver Code
public static void Main()
{
Console.Write(isPalindrome(1001) ? "Yes, it is Palindrome" : "No, not Palindrome");
}
}
// This code is contributed by Saurabh Jaiswal
Javascript
输出
Yes, it is Palindrome
时间复杂度: O(K),其中K 是位数
辅助方法: O(1)