반응형
주어진 숫자를 자릿수 별로 쪼개는 문제다.
일의 자리부터 계산하고 떼버리고?를
0이 될때까지 한다.
곱하는 건 초기값이 1, 더하는 건 0으로 ~
class Solution {
public:
int subtractProductAndSum(int n) {
int product = 1;
int sum = 0;
while(n!=0){
product *= n%10;
sum += n%10;
n /=10;
}
return product-sum;
}
};
반응형
'IT > 알고리즘' 카테고리의 다른 글
11] Leetcode 1295. Find Numbers with Even Number of Digits (0) | 2020.06.24 |
---|---|
10] Leetcode 1389. Create Target Array in the Given Order (0) | 2020.06.23 |
8] Leetcode 1313. Decompress Run-Length Encoded List (0) | 2020.06.21 |
7] Leetcode 1365. How Many Numbers Are Smaller Than the Current Number (0) | 2020.06.21 |
6] Leetcode 771. Jewels and Stones (0) | 2020.06.21 |
댓글