Reverse digits of an integer.
创新互联建站主营龙潭网站建设的网络公司,主营网站建设方案,
重庆APP开发公司,龙潭h5
小程序开发搭建,龙潭网站营销推广欢迎龙潭等地区企业咨询
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
1.先记录符号
2.最后检查是否溢出int的范围
- class Solution {
- public int reverse(int x) {
- boolean flag=(x>=0)?true:false;
- long data=1;
- int[] arr=new int[10];
- int index=0;
- x=Math.abs(x);
- while(x>0){
- arr[index]=x % 10;
- x=x/10;
- index++;
- }
-
- long result=0L;
- for(int i=index-1;i>=0;i--){
- result=result+arr[i]*data;
- data=data*10;
- }
- if(result>Integer.MAX_VALUE || result
- return 0;
- }
- return flag==true?(int)result:-(int)result;
- }
- }
本文题目:ReverseInteger-创新互联
浏览路径:
http://cxhlcq.com/article/hojsg.html