zoukankan      html  css  js  c++  java
  • [LeetCode] 7

    Reverse digits of an integer.

    Example1: x = 123, return 321
    Example2: x = -123, return -321

    class Solution {
    public:
      int reverse(int x) {
        int res = 0;
        int x0 = x;
        int x1;
        while (x0) {
          x1 = x0 % 10;
          x0 = x0 / 10;
          if (res > (int)(0x7fffffff)/10) {  // 如果数据发生溢出
            return 0;
          }
          if( res < (int)(0x80000000)/10 ) {
            return 0;
          }
          res = res * 10 + x1;
        }
        return res;
      }
    };

  • 相关阅读:
    20201107
    20201024
    20201020
    20200331
    20200330
    20200320
    20200319
    20200310
    20200221
    20190926
  • 原文地址:https://www.cnblogs.com/shoemaker/p/4765891.html
Copyright © 2011-2022 走看看