# 7.Reverse Integer

**7.Reverse Integer**

难度:Easy

> 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
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
```

talk less, show the code:

```
class Solution {
public:
    int reverse(int x) {
        string s=to_string(x);
        int flag=(s[0]=='-');
        long int res=0;
        for(int i=s.length()-1;i>=flag;i--)
            res= res*10 + int(s[i]-'0');
        return res>INT_MAX ? 0 : (flag? -res: res);
            
    }
};
```

> 执行用时 :4 ms, 在所有 C++ 提交中击败了87.79%的用户\
> 内存消耗 :8.3 MB, 在所有 C++ 提交中击败了77.33%的用户


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dfine.gitbook.io/leetcode/7.reverse_integer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
