Leetcode Note: Go - Subtract the Product and Sum of Digits of an Integer

Subtract the Product and Sum of Digits of an Integer - LeetCode
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/

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

回答

Subtract the Product and Sum of Digits of an Integer - LeetCode
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/solutions/483456/golang-0ms-o-n-time-o-1-space-no-strconv/?orderBy=most_votes&languageTags=golang

func subtractProductAndSum(n int) int {
  sum, product := 0, 1
  
  for n > 0 {
    mod := n % 10
    sum += mod
    product *= mod
    
    n /= 10
  }
  
  return product - sum
}