MENU

LeetCode7. 反转整数

July 8, 2018 • Read: 3087 • LeetCode阅读设置

一位一位取出 x,最后判断是否超出 int 的范围即可

  • class Solution {
  • public int reverse(int x) {
  • long ans = 0;
  • int MIN_INT = 0x80000000;
  • int MAX_INT = 0x7fffffff;
  • while(x != 0) {
  • ans = ans * 10 + (x % 10);
  • x /= 10;
  • }
  • if(ans < MIN_INT || ans > MAX_INT)
  • ans = 0;
  • return (int)ans;
  • }
  • }
Last Modified: May 12, 2021
Archives Tip
QR Code for this page
Tipping QR Code
Leave a Comment