Leetcode Note: Go - Sum of Digits of String After Convert

Sum of Digits of String After Convert - LeetCode
https://leetcode.com/problems/sum-of-digits-of-string-after-convert/

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

回答

With Go fast 100% - Sum of Digits of String After Convert - LeetCode
https://leetcode.com/problems/sum-of-digits-of-string-after-convert/solutions/1532750/with-go-fast-100/

func getLucky(s string, k int) int {
    str := ""
	sum := 0
	for i := 0; i < len(s); i++ {
		n := int(s[i]) - 96
		str += strconv.Itoa(n)
	}

	for j := 0; j < k; j++ {
		sum = 0
		for i := 0; i < len(str); i++ {
			n, _ := strconv.Atoi(string(str[i]))
			for n > 0 {
				res := n % 10
				n /= 10
				sum += res
			}
		}
		str = strconv.Itoa(sum)
	}
	return sum
}