当前位置:网站首页>LeetCode 1281. Difference of sum of bit product of integer

LeetCode 1281. Difference of sum of bit product of integer

2022-06-24 03:39:00 freesan44

Title address (1281. The difference between the sum of the products of integers )

https://leetcode-cn.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/

Title Description

 Give you an integer  n, Please help calculate and return the integer 「 The product of your numbers 」 And 「 The sum of your numbers 」 Difference .

 

 Example  1:

 Input :n = 234
 Output :15 
 explain :
 Product of numbers  = 2 * 3 * 4 = 24 
 The sum of your numbers  = 2 + 3 + 4 = 9 
 result  = 24 - 9 = 15


 Example  2:

 Input :n = 4421
 Output :21
 explain : 
 Product of numbers  = 4 * 4 * 2 * 1 = 32 
 The sum of your numbers  = 4 + 4 + 2 + 1 = 11 
 result  = 32 - 11 = 21


 

 Tips :

1 <= n <= 10^5

Ideas

Violence law

Code

  • Language support :Python3

Python3 Code:

class Solution:
    def subtractProductAndSum(self, n: int) -> int:
        sList = list(map(int,list(str(n))))
        # print(sList)
        ji = 1
        he = 0
        for i in sList:
            ji *= i
            he += i
        return ji-he

Complexity analysis

Make n Is array length .

  • Time complexity :$O(n)$
  • Spatial complexity :$O(1)$
原网站

版权声明
本文为[freesan44]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/09/20210922194430822f.html