LeetCode第7题,难度简单。
原题地址:https://leetcode-cn.com/problems/reverse-integer/
题目描述:
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
示例 1:
输入: 123
输出: 321
解题思路:
先讲不用字符串的方式。
当然如果使用字符串形式的话,会简单很多。
中文官网题解:
https://leetcode-cn.com/problems/reverse-integer/solution/
个人题解:
public class Solution {
public int reverse(int x) {
if(0 <= x && x < 10)
return x;
long result = 0l;
int flag = x < 0 ? -1 : 1;
int y = Math.abs(x);
double i = 0d;
while(y>0){i++;y/=10;}
x = Math.abs(x);
while(i >= 0){
result += (x % 10) * Math.pow(10D,i-1);
i--;
x /= 10;
}
return result * flag > 2147483647 || result * flag < -2147483648 ? 0 : (int)result * flag;
}
}
结果:
结果那自然是相当的不理想了。而且数据并没有从英文网同步过来...
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有