1281_SubtracttheProductandSumofDigitsofanInteger - a920604a/leetcode GitHub Wiki


title: 1281. Subtract the Product and Sum of Digits of an Integer categories: leetcode comments: false

problem

solution

class Solution {
public:
    int subtractProductAndSum(int n) {
        int prod = 1, sum = 0;
        while(n){
            prod *= (n%10);
            sum+=(n%10);
            n/=10;
        }
        return prod - sum;
    }
};

analysis

  • time complexity O(logn)
  • sparse complexity O(1)