---
# System prepended metadata

title: 7. Reverse Integer
tags: [Leetcode]

---

---
tags: Leetcode
topic: 7. Reverse Integer
---
# 7. Reverse Integer

## Solution 直覺寫法
```c=
#define MAX_INT (int)((unsigned)(-1)>>1)
#define MIN_INT (int)(~MAX_INT)

int reverse(int x){
    if(x>=MAX_INT||x<=MIN_INT)
        return 0;
    int pos = (x>>31);
    int ans =0;
    x =pos?x*(-1):x;
    while(x>9){
        ans = ans*10 + x%10;
        x=x/10;
    }
    if(ans>(MAX_INT/10))
        return 0;
    ans = ans*10 + x%10;
    ans = pos?ans*(-1):ans;
    return ans;
}
```
![](https://i.imgur.com/XfTTJsN.png)
