处理溢出的整数的反转数字 |设置 2
给定一个32 位整数N 。任务是反转N ,如果反转的整数溢出,则打印-1作为输出。
例子
Input: N = 123
Output: 321
Input: N = -123
Output: -321
Input: N = 120
Output: 21
方法:与文章的方法 Set 1 不同,这个问题可以简单地通过使用64 位数据结构和 int 数据类型范围[-2^31, 2^31 – 1]来解决
- 将给定数字N的值复制到 long 变量中
- 检查是否为负
- 现在逐位反转长数字,如果在任何一步,它的值超出了 int 类型的范围,则返回0 。
- 如果给定数为负数,则使结果数为负数
- 再次检查数字是否在 int 范围内。如果是返回0 ,否则返回反转的数字。
下面是上述方法的实现。
C++
// C++ program for above approach
#include
// Function to reverse digits in a number
int reverseDigits(int N)
{
// Taking a long variable
// to store the number
long m = N;
int neg = m < 0 ? -1 : 1;
if (m * neg < pow(-2, 31)
|| m * neg >= pow(2, 31))
return 0;
m = m * neg;
long n = 0;
while (m > 0) {
if (n * 10 < pow(-2, 31)
|| n * 10 >= pow(2, 31))
return 0;
n = n * 10 + m % 10;
m = m / 10;
}
if (n * neg < pow(-2, 31)
|| n * neg >= pow(2, 31))
return 0;
n = n * neg;
return n;
}
// Driver Code
int main()
{
int N = 5896;
printf("Reverse of no. is %d",
reverseDigits(N));
return 0;
}
Java
// Java program for above approach
class GFG {
// Function to reverse digits in a number
static int reverseDigits(int N) {
// Taking a long variable
// to store the number
long m = N;
int neg = m < 0 ? -1 : 1;
if (m * neg < Math.pow(-2, 31)
|| m * neg >= Math.pow(2, 31))
return 0;
m = m * neg;
long n = 0;
while (m > 0) {
if (n * 10 < Math.pow(-2, 31)
|| n * 10 >= Math.pow(2, 31))
return 0;
n = n * 10 + m % 10;
m = m / 10;
}
if (n * neg < Math.pow(-2, 31)
|| n * neg >= Math.pow(2, 31))
return 0;
n = n * neg;
return (int) n;
}
// Driver Code
public static void main(String args[]) {
int N = 5896;
System.out.println("Reverse of no. is " + reverseDigits(N));
}
}
// This code is contributed by Saurabh Jaiswal
Python3
# Python code for the above approach
# Function to reverse digits in a number
def reverseDigits(N):
# Taking a long variable
# to store the number
m = N
neg = -1 if m < 0 else 1
if (m * neg < (-2 ** 31) or m * neg >= (2 ** 31)):
return 0
m = m * neg
n = 0
while (m > 0):
if (n * 10 < (-2 ** 31) or n * 10 >= (2 ** 31)):
return 0
n = n * 10 + m % 10
m = (m // 10)
if (n * neg < (-2 ** 31) or n * neg >= (2 ** 31)):
return 0
n = n * neg
return n
# Driver Code
N = 5896
print(f"Reverse of no. is {reverseDigits(N)}")
# This code is contributed by gfgking
C#
// C# program for above approach
using System;
class GFG
{
// Function to reverse digits in a number
static int reverseDigits(int N)
{
// Taking a long variable
// to store the number
long m = N;
int neg = m < 0 ? -1 : 1;
if (m * neg < Math.Pow(-2, 31)
|| m * neg >= Math.Pow(2, 31))
return 0;
m = m * neg;
long n = 0;
while (m > 0) {
if (n * 10 < Math.Pow(-2, 31)
|| n * 10 >= Math.Pow(2, 31))
return 0;
n = n * 10 + m % 10;
m = m / 10;
}
if (n * neg < Math.Pow(-2, 31)
|| n * neg >= Math.Pow(2, 31))
return 0;
n = n * neg;
return (int)n;
}
// Driver Code
public static void Main()
{
int N = 5896;
Console.Write("Reverse of no. is " + reverseDigits(N));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
Reverse of no. is 6985
时间复杂度: O(D),其中 D 是 N 中的位数。
辅助空间: O(1)