【007-Reverse Integer(翻转整数)】

来源:互联网 发布:淘宝改高价 编辑:程序博客网 时间:2024/05/20 07:17

原题

  Reverse digits of an integer. 
  Example1: x = 123, return 321 
  Example2: x = -123, return -321 

题目大意

  输入一个整数对其进行翻转 

解题思路

  通过求余数求商法进行操作。 


public class Solution

{

public intreverse(intx)

{

long tmp = x;

long result = 0;

while(tmp !=0)

{

result =result * 10 + tmp %10;

tmp =tmp / 10;

}

if(result <Integer.MIN_VALUE ||result > Integer.MAX_VALUE)

{

result =0;

}

return (int)result;

}

}

0 0
原创粉丝点击