-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 int) int { | ||
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 的余数,就能得到反转后的数。这个题目里需要注意的一点是边界判断,要注意反转后溢出的情况。 | ||
|
||
|
137d507
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to following URLs: