写的不是很好,仅记录自己所写的,仅供参考。

第七题:

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

public class Solution {
    public int reverse(int x) {
        int newX = 0;
        int flag=0;
        if(x<0){
            x=-x;
            flag=-1;
        }
        while((x*10)/10!=0){
            if (newX>(Integer.MAX_VALUE-x%10)/10){  
                return 0;  
            }  
            newX = newX*10 + x%10;
            x=x/10;
        }
        if(flag==-1){
            return -newX;
        }
        return newX;
    }
}

相关文章:

  • 2021-07-20
  • 2021-09-06
  • 2022-01-09
  • 2021-10-24
  • 2021-11-15
  • 2022-01-08
  • 2022-12-23
  • 2021-10-22
猜你喜欢
  • 2021-05-15
  • 2022-12-23
  • 2022-12-23
  • 2021-07-03
  • 2021-10-22
  • 2021-06-15
  • 2021-06-13
相关资源
相似解决方案