diff options
author | terminaldweller <thabogre@gmail.com> | 2022-07-25 04:40:50 +0000 |
---|---|---|
committer | terminaldweller <thabogre@gmail.com> | 2022-07-25 04:40:50 +0000 |
commit | 95f49936aef730113f768190ff7fd8baef8fb6ad (patch) | |
tree | 2cb76244b12d3fa69c85b900118b77600dfcb9f4 | |
parent | updates (diff) | |
download | leetcode-95f49936aef730113f768190ff7fd8baef8fb6ad.tar.gz leetcode-95f49936aef730113f768190ff7fd8baef8fb6ad.zip |
updates
-rw-r--r-- | 7/go.mod | 3 | ||||
-rw-r--r-- | 7/main.go | 33 |
2 files changed, 36 insertions, 0 deletions
diff --git a/7/go.mod b/7/go.mod new file mode 100644 index 0000000..b924aab --- /dev/null +++ b/7/go.mod @@ -0,0 +1,3 @@ +module 7 + +go 1.17 diff --git a/7/main.go b/7/main.go new file mode 100644 index 0000000..7bf96b8 --- /dev/null +++ b/7/main.go @@ -0,0 +1,33 @@ +package main + +import "fmt" + +func reverse(x int) int { + var res int + var negative bool + if x < 0 { + negative = true + x = x * -1 + } + for x > 0 { + digit := x - ((x / 10) * 10) + res = res*10 + digit + if res > (0x1 << 31) { + return 0 + } + x = x / 10 + } + + if negative { + return res * -1 + } + return res +} + +func main() { + fmt.Println(reverse(123)) + fmt.Println(reverse(-123)) + fmt.Println(reverse(120)) + fmt.Println(reverse(1534236469)) + +} |