首页 > 代码库 > leetcode 7. Reverse Integer

leetcode 7. Reverse Integer

7. Reverse Integer

技术分享

 

 

(略坑:) )

 

public class Solution {        public int reverse(int x) {            boolean flag = (x < 0)? true:false;            if(flag)             x = -x;            int rec = 0;            int res = 0;            while(x != 0)            {                res = res*10 + x%10;                rec = x;                x /= 10;                System.out.println(res);            }                                    if(res % 10 != rec)            return 0;            if(flag)             return -res;            return res;                    }}

使用变量rec只是为了判断一下是否越界。

 

leetcode 7. Reverse Integer