Leetcode Note: Go - Base 7

Base 7 - LeetCode
https://leetcode.com/problems/base-7/

  • Go 言語で取り組んだメモ

所感

  • 7 進数

回答

Go lang - LeetCode Discuss
https://leetcode.com/problems/base-7/discuss/1583557/Go-lang

func convertToBase7(num int) string {
	if num == 0 {
		return "0"
	}
	return fmt.Sprintf("%d", TtoS(Abs(num))*Abs(num)/num)
}

func TtoS(num int) int {
	divided := int(math.Ceil(float64(num / 7)))

	if num < 7 {
		return num
	}
	return TtoS(divided)*10 + num%7
}

func Abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}