码迷,mamicode.com
首页 > 其他好文 > 详细

#Leetcode# 7. Reverse Integer

时间:2018-11-02 23:59:48      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:amp   ret   lse   tps   dig   des   else   code   tco   

https://leetcode.com/problems/reverse-integer/description/

 

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21

代码:

class Solution {
public:
    int reverse(int x) {
        long long ans = 0;
    int flag = 1;
    if(x < 0) {
        flag = -1;
        x = abs(x);
    }
    while(x > 0) {
        ans = ans * 10 + x % 10;
        x /= 10;
    }
    if(ans > INT_MAX) return 0;
    else if(flag < 0) return -ans;
    else return ans;
    }
};

  

#Leetcode# 7. Reverse Integer

标签:amp   ret   lse   tps   dig   des   else   code   tco   

原文地址:https://www.cnblogs.com/zlrrrr/p/9898484.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!