반응형
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/description/
Subtract the Product and Sum of Digits of an Integer - LeetCode
Can you solve this real interview question? Subtract the Product and Sum of Digits of an Integer - Given an integer number n, return the difference between the product of its digits and the sum of its digits. Example 1: Input: n = 234 Output: 15 Explana
leetcode.com
간단 구현문제였습니다.
📕 풀이방법
📔 입력 및 초기화
1. n의 각 자리를 list로 저장할 num을 선언 후 int형으로 자료형 변환을 해줍니다.2. 각 자리의 합 sum, 곱 product를 선언 후 각각 0, 1로 초기화해줍니다.
📔 풀이과정
큰 수 곱셈을 위해 python을 선택했습니다.num에 대해 for loop를 수행하며 각자리 수의 곱과 합을 누적해 줍니다.
📔 정답 출력 | 반환
product - sum값을 반환합니다.
📕 Code
📔 Python
class Solution(object):
def subtractProductAndSum(self, n):
num = list(map(int,str(n)))
product = 1
sum = 0
for i in num:
product *= i
sum += i
return product - sum
*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.