一、题目内容

image.png

二、题解

解法1:

思路

long 接一下,防止溢出

代码

  1. public class Solution {
  2. public int reverse (int x) {
  3. // write code here
  4. long ans = 0;
  5. while(x!=0){
  6. int c = x%10;
  7. ans = ans*10+c;
  8. x/=10;
  9. }
  10. if(ans>Integer.MAX_VALUE || ans<Integer.MIN_VALUE){
  11. return 0;
  12. }
  13. return (int)ans;
  14. }
  15. }