输入一个带符号的整数,得到它的相反数:

 

例1:

输入: 123
 输出:   321

 

例2:

输入: -123
 输出: -321

 

例3:

输入: 120
 输出: 21


public int reserve(int x){
  int result = 0;
  while(x!=0){
    int tail = x % 10;
    int newResult = result * 10 + tail;
    if((newResult-tail)/10 != result){
      return 0;
    }
    result = newResult;
    x = x / 10;
  }
  return result;
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2020-06-23
  • 2022-12-23
  • 2021-06-20
  • 2021-12-14
  • 2022-12-23
  • 2021-11-04
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-03
  • 2022-02-03
相关资源
相似解决方案