mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 08:27:30 +08:00
28
leetcode/0504.Base-7/504.Base 7.go
Normal file
28
leetcode/0504.Base-7/504.Base 7.go
Normal file
@ -0,0 +1,28 @@
|
||||
package leetcode
|
||||
|
||||
import "strconv"
|
||||
|
||||
func convertToBase7(num int) string {
|
||||
if num == 0 {
|
||||
return "0"
|
||||
}
|
||||
negative := false
|
||||
if num < 0 {
|
||||
negative = true
|
||||
num = -num
|
||||
}
|
||||
var ans string
|
||||
var nums []int
|
||||
for num != 0 {
|
||||
remainder := num % 7
|
||||
nums = append(nums, remainder)
|
||||
num = num / 7
|
||||
}
|
||||
if negative {
|
||||
ans += "-"
|
||||
}
|
||||
for i := len(nums) - 1; i >= 0; i-- {
|
||||
ans += strconv.Itoa(nums[i])
|
||||
}
|
||||
return ans
|
||||
}
|
46
leetcode/0504.Base-7/504.Base 7_test.go
Normal file
46
leetcode/0504.Base-7/504.Base 7_test.go
Normal file
@ -0,0 +1,46 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question504 struct {
|
||||
para504
|
||||
ans504
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
type para504 struct {
|
||||
num int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
type ans504 struct {
|
||||
ans string
|
||||
}
|
||||
|
||||
func Test_Problem504(t *testing.T) {
|
||||
|
||||
qs := []question504{
|
||||
|
||||
{
|
||||
para504{100},
|
||||
ans504{"202"},
|
||||
},
|
||||
|
||||
{
|
||||
para504{-7},
|
||||
ans504{"-10"},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 504------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans504, q.para504
|
||||
fmt.Printf("【input】:%v ", p.num)
|
||||
fmt.Printf("【output】:%v \n", convertToBase7(p.num))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
60
leetcode/0504.Base-7/README.md
Normal file
60
leetcode/0504.Base-7/README.md
Normal file
@ -0,0 +1,60 @@
|
||||
# [504. Base 7](https://leetcode-cn.com/problems/base-7/)
|
||||
|
||||
## 题目
|
||||
|
||||
Given an integer num, return a string of its base 7 representation.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
Input: num = 100
|
||||
Output: "202"
|
||||
|
||||
**Example 2:**
|
||||
|
||||
Input: num = -7
|
||||
Output: "-10"
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- -10000000 <= num <= 10000000
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个整数 num,将其转化为 7 进制,并以字符串形式输出。
|
||||
|
||||
## 解题思路
|
||||
|
||||
num反复除以7,然后倒排余数
|
||||
|
||||
# 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
import "strconv"
|
||||
|
||||
func convertToBase7(num int) string {
|
||||
if num == 0 {
|
||||
return "0"
|
||||
}
|
||||
negative := false
|
||||
if num < 0 {
|
||||
negative = true
|
||||
num = -num
|
||||
}
|
||||
var ans string
|
||||
var nums []int
|
||||
for num != 0 {
|
||||
remainder := num % 7
|
||||
nums = append(nums, remainder)
|
||||
num = num / 7
|
||||
}
|
||||
if negative {
|
||||
ans += "-"
|
||||
}
|
||||
for i := len(nums) - 1; i >= 0; i-- {
|
||||
ans += strconv.Itoa(nums[i])
|
||||
}
|
||||
return ans
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user