使用按位移位反转数字

发布于 2024-12-05 03:19:03 字数 575 浏览 1 评论 0原文

我正在尝试找到一种方法来反转数字而不

  1. 将其转换为字符串以查找长度
  2. 反转字符串并将其解析回来
  3. 运行一个单独的循环来计算长度

我目前正在这样做

 public static int getReverse(int num){
        int revnum =0;
        for( int i = Integer.toString(num).length() - 1 ; num>0 ; i-- ){
            revnum += num % 10 * Math.pow( 10 , i );
            num /= 10;
        }
        return revnum;        
    }

但我想实现上述3个条件。

我正在寻找一种方法,可能使用按位移位运算符或某种其他类型的按位运算。

是否可以 ?如果是这样怎么办?

PS:如果给出 1234 作为输入,它应该返回 4321。我只会反转整数和长整型

I am trying to find a way to reverse a number without

  1. Converting it to a string to find the length
  2. Reversing the string and parsing it back
  3. Running a separate loop to compute the Length

i am currently doing it this way

 public static int getReverse(int num){
        int revnum =0;
        for( int i = Integer.toString(num).length() - 1 ; num>0 ; i-- ){
            revnum += num % 10 * Math.pow( 10 , i );
            num /= 10;
        }
        return revnum;        
    }

But I would Like to implement the above 3 conditions.

I am looking for a way , possibly using the bit wise shift operators or some other kind of bitwise operation.

Is it possible ? If so how ?

PS : If 1234 is given as input it should return 4321. I will only be reversing Integers and Longs

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久随 2024-12-12 03:19:03

怎么样:

int revnum = 0;
while (num != 0) {
  revnum = revnum * 10 + (num % 10);
  num /= 10;
}
return revnum;

代码需要一个非负输入。

这对您来说可能很重要,也可能不重要,但值得注意的是,getReverse(getReverse(x)) 不一定等于x,因为它不会保留尾随零。

How about:

int revnum = 0;
while (num != 0) {
  revnum = revnum * 10 + (num % 10);
  num /= 10;
}
return revnum;

The code expects a non-negative input.

This may or may not matter to you, but it's worth noting that getReverse(getReverse(x)) does not necessarily equal x as it won't preserve trailing zeroes.

懵少女 2024-12-12 03:19:03

这个怎么样?它也处理负数。

public int getReverse(int num){
   int rst=0;
   int sign;
   sign=num>0?1:-1;

   num*=sign;
   while(num>0){
      int lastNum = num%10;
      rst=rst*10+lastNum
      num=num/10;
   }
   return rst*sign;
}

How about this? It handles negative numbers as well.

public int getReverse(int num){
   int rst=0;
   int sign;
   sign=num>0?1:-1;

   num*=sign;
   while(num>0){
      int lastNum = num%10;
      rst=rst*10+lastNum
      num=num/10;
   }
   return rst*sign;
}
神魇的王 2024-12-12 03:19:03

任何解决方案中都根本不考虑 Integer.MAX_VALUEInteger.MIN_VALUE

例如:如果输入为 -21474836472147483647,则 o/p 将分别为 1126087180-1126087180

请尝试以下处理两个条件的解决方案,因此如果在任何时间点数字超出边界条件,即 INPUT_VALUE > Integer.MAX_VALUE 或 INPUT_VALUE < Integer.MIN_VALUE 它将返回 0

class ReversingIntegerNumber {
    
    public int reverse(int originalNum) {
        boolean originalIsNegative = originalNum > 0 ? false : true;
        int reverseNum = 0;
        int modValue;
        originalNum = Math.abs(originalNum);
        while(originalNum != 0) {
            modValue = originalNum % 10;
            if(reverseNum > (Integer.MAX_VALUE - modValue)/10) {
                return 0;
            }
            reverseNum = (reverseNum * 10) + modValue;
            originalNum /= 10;
        }
        return originalIsNegative ? -1 * reverseNum : reverseNum;
    }
}

Integer.MAX_VALUE or Integer.MIN_VALUE is not at all considered in any of the solutions.

For eg: if the input is -2147483647 or 2147483647 the o/p will be 1126087180 and -1126087180 respectively.

Please try the below solutions where both the conditions are handled, so if at any point of time the number is going beyond the boundary conditions i.e., INPUT_VALUE > Integer.MAX_VALUE or INPUT_VALUE < Integer.MIN_VALUE it would return 0

class ReversingIntegerNumber {
    
    public int reverse(int originalNum) {
        boolean originalIsNegative = originalNum > 0 ? false : true;
        int reverseNum = 0;
        int modValue;
        originalNum = Math.abs(originalNum);
        while(originalNum != 0) {
            modValue = originalNum % 10;
            if(reverseNum > (Integer.MAX_VALUE - modValue)/10) {
                return 0;
            }
            reverseNum = (reverseNum * 10) + modValue;
            originalNum /= 10;
        }
        return originalIsNegative ? -1 * reverseNum : reverseNum;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文