スポンサーリンク

【LeetCode】1281. Subtract the Product and Sum of Digits of an Integer 解答・解説【Python】

スポンサーリンク
スポンサーリンク
この記事は約2分で読めます。

 

 

問題

原文

Given an integer number n, return the difference between the product of its digits and the sum of its digits.

 

Example 1:

Example 2:

 

Constraints:

  • 1 <= n <= 10^5

 

内容

整数nが与えられるので、各桁の積と和の差を返してください。

 

制約からnは最大10^5の桁数があるので、時間計算量O(n)の解法では10^5の計算量に収まる。

 

※正しくない可能性があります。

 

解答

解答1:Python, brute force

 

 

最初にnを文字列に変換しているので、for文の内部で毎回intに変換しているのは改善の余地がありそう。

 

解答2:

 

 

 

メモ・参考・感想

 

 

 

前:191. Number of 1 Bits

次:404. Sum of Left Leaves

 

LeetCode 解答・解説記事一覧

コメント