Notice
Recent Posts
Recent Comments
Today
Total
01-10 22:06
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[LEETCODE] 1281. Subtract the Product and Sum of Digits of an Integer ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[LEETCODE] 1281. Subtract the Product and Sum of Digits of an Integer

WonderJay 2022. 10. 10. 14:42
728x90
๋ฐ˜์‘ํ˜•
SMALL

https://leetcode.com/

TITLE : 1281. Subtract the Product and Sum of Digits of an Integer

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

 

๊ฐ ์ž๋ฆฟ์ˆ˜๋“ค์˜ ๊ณฑ๊ณผ ๊ฐ ์ž๋ฆฟ์ˆ˜๋“ค์˜ ํ•ฉ์˜ ์ฐจ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด๋œ๋‹ค

.

[C++]

class Solution {
public:
    int subtractProductAndSum(int n) {
        int pod = 1; int sod = 0; int t = 0;
        string temp = to_string(n);
        vector<int> v;
        for (char ele : temp) {
            pod *= (ele - '0');
            sod += (ele - '0');
        }
        return pod - sod;
    }
};

๊ฐ ์ž๋ฆฟ์ˆ˜๋งŒํผ ์ˆœํšŒํ•˜๋ฏ€๋กœ O(log(n)) ๋ฒˆ์˜ ์—ฐ์‚ฐ์ด ํ•„์š”ํ•˜๋‹ค.

 

์‹œ๊ฐ„ ๋ณต์žก๋„ : O(logN)

728x90
๋ฐ˜์‘ํ˜•
LIST
Comments