Skip to content

Commit

Permalink
feat: reverse integer
Browse files Browse the repository at this point in the history
  • Loading branch information
leverz committed Nov 19, 2019
1 parent 184fb6d commit 137d507
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
|297|[二叉树的序列化与反序列化](https://leetcode-cn.com/problems/serialize-and-deserialize-binary-tree/) | [Golang](./content/blog/algorithm/297.md)|
|99|[恢复二叉搜索树](https://leetcode-cn.com/problems/recover-binary-search-tree/)| [Golang](./content/blog/algorithm/99.md)|
|35|[搜索插入位置](https://leetcode-cn.com/problems/search-insert-position/)| [Golang](./content/blog/algorithm/35.md)|
|7|[整数反转](https://leetcode-cn.com/problems/reverse-integer/)| [Golang](./content/blog/algorithm/7.md)|
|6|[Z 字形变换](https://leetcode-cn.com/problems/zigzag-conversion/submissions/)| [Golang](./content/blog/algorithm/6.md)|
|5|[最长回文子串](https://leetcode-cn.com/problems/longest-palindromic-substring/)| [Golang](./content/blog/algorithm/5.md)|
|4|[寻找两个有序数组的中位数](https://leetcode-cn.com/problems/median-of-two-sorted-arrays/)| [Golang](./content/blog/algorithm/4.md)|
Expand Down
29 changes: 29 additions & 0 deletions content/blog/algorithm/7.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
title: 7. 整数反转
date: '2019-11-19T21:39:00.000Z'
---

[https://leetcode-cn.com/problems/reverse-integer/](https://leetcode-cn.com/problems/reverse-integer/)

使用语言:Golang

```Go
func reverse(x intint {
    MIN := -2147483648
    MAX := 2147483647
    var res int
    for x != 0 {
        if res * 10 < MIN || res * 10 > MAX {
            return 0
        }
        y := x % 10
        x = x / 10
        res = res * 10 + y
    }
    return res
}
```

题目很简单,反转方式就是原数不断除以 10,新数不断乘以 10,再加上原数除以 10 的余数,就能得到反转后的数。这个题目里需要注意的一点是边界判断,要注意反转后溢出的情况。


1 comment on commit 137d507

@vercel
Copy link

@vercel vercel bot commented on 137d507 Nov 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.