#Leetcode# 7. Reverse Integer

Posted 丧心病狂工科女

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了#Leetcode# 7. Reverse Integer相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

leetcode | Reverse Nodes in k-Group

[LeetCode] Reverse Words in a String II

LeetCode Reverse Words in a String III

Leetcode 151. Reverse Words in a String

LeetCode - 25. Reverse Nodes in k-Group

LeetCode:Reverse Words in a String