Golang | Leetcode Golang题解之第553题最优除法
题目:
题解:
func optimalDivision(nums []int) string {
n := len(nums)
if n == 1 {
return strconv.Itoa(nums[0])
}
if n == 2 {
return fmt.Sprintf("%d/%d", nums[0], nums[1])
}
ans := &strings.Builder{}
ans.WriteString(fmt.Sprintf("%d/(%d", nums[0], nums[1]))
for _, num := range nums[2:] {
ans.WriteByte('/')
ans.WriteString(strconv.Itoa(num))
}
ans.WriteByte(')')
return ans.String()
}