๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/Leetcode

[Leetcode,c++] Add Digits

by dkswnkk 2022. 3. 2.

๋ฌธ์ œ

https://leetcode.com/problems/add-digits/

 

Add Digits - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

์ฝ”๋“œ

(1) ์žฌ๊ท€, ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•œ ํ’€์ด

class Solution {
public:
    int addDigits(int num) {
        int temp1=0, temp2=0, ans=num;
        while(true){
            while(num>9){
                temp1 = num%10;
                temp2 = num/10;
                ans = temp1 + temp2;
                num = ans;
            }
            if(num<10){
                num = ans;
                return num;
            }
        }
        
    }
};

 

(2) ์กฐ๊ฑด์‹์„ ํ™œ์šฉํ•˜์—ฌ O(1) ์‹œ๊ฐ„ ๋ณต์žก๋„๋กœ ํ•ด๊ฒฐํ•œ ํ’€์ด

class Solution {
public:
    int addDigits(int num) {
      if(num==0)
          return 0;
      else if(num%9==0)
          return 9;
      else
          return num%9;
    }
};

 

ํ’€์ด

์ด ๋ฌธ์ œ๋Š” 2^31- 1๊นŒ์ง€ ๋“ค์–ด์˜ค๋Š” ์ž์—ฐ์ˆ˜์˜ ๊ฐ ์ž๋ฆฟ์ˆ˜๋ฅผ ํ•œ ์ž๋ฆฟ์ˆ˜๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ๋”ํ•ด์„œ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. 

์ฒ˜์Œ์—๋Š” ๋ฐ˜๋ณต๋ฌธ์„ ํ™œ์šฉํ•ด์„œ ๊ฐ ์ž๋ฆฟ์ˆ˜๋ฅผ ๋–ผ์–ด๊ฐ€๋ฉฐ ํ’€์—ˆ์ง€๋งŒ ๋ฌธ์ œ์—์„œ Follow up: Could you do it without any loop/recursion in O(1) runtime? ๋ผ๋Š” ์กฐ๊ฑด์„ ์ค€ ๊ฒƒ์„ ๋ณด๋ฉด ๋ฐ˜๋ณต๋ฌธ์ด๋‚˜ ์žฌ๊ท€๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  O(1)์˜ ์‹œ๊ฐ„์œผ๋กœ ํ•ด๊ฒฐํ•˜๋ผ๋Š” ๋ฌธ์ œ์ด๊ธฐ์— ์ข€ ๋” ๊ณ ๋ฏผ์ด ํ•„์š”ํ–ˆ๋˜ ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค. 

๊ฒฐ๊ณผ์ ์œผ๋กœ O(1)๋กœ ํ•ด๊ฒฐํ•œ ๋‘ ๋ฒˆ์งธ ํ’€์ด๋ฅผ ๋ณด๋ฉด ์•Œ๊ฒ ์ง€๋งŒ 9๋กœ ๋ชจ๋“ˆ๋Ÿฌ ์—ฐ์‚ฐ์„ ํ–ˆ์„ ๋•Œ์˜ ๊ฐ’์ด ๊ฒฐ๊ณผ์ ์œผ๋กœ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๊ฐ’์ด ๋จ์„ ์•Œ ์ˆ˜ ์žˆ๋Š”๋ฐ ๊ทธ ์ด์œ ๋Š” ์ˆซ์ž 9๋ผ๋Š” ๊ฐ’์„ ์ž์„ธํžˆ ์‚ดํŽด๋ณด๋ฉด ์•„๋ž˜์˜ ํŠน์ง•์„ ์ง€๋‹ˆ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์ถœ์ฒ˜: ์œ„ํ‚คํ”ผ๋””์•„

4๋ฒˆ์งธ ํŠน์ง•์„ ์ž˜ ์‚ดํŽด๋ณด๋ฉด 9์— ์–ด๋–ค ์ˆ˜๋ฅผ ๊ณฑํ•˜๋”๋ผ๋„, ๋‚˜์˜ค๋Š” ๊ฐ’์˜ ๊ฐ ์ž๋ฆฌ ์ˆซ์ž๋ฅผ ๋ชจ๋‘ ํ•ฉํ•˜๋ฉด 9์˜ ๋ฐฐ์ˆ˜ ๊ฐ’์ด ๋ฉ๋‹ˆ๋‹ค. 

else if(num%9==0) return 9;

๋”ฐ๋ผ์„œ ์œ„ ์ฝ”๋“œ ์กฐ๊ฑด ๋ถ€๋ถ„์€ 9๋กœ mod์—ฐ์‚ฐ์„ ๊ณ„์†ํ•ด์ฃผ์—ˆ์„ ๋•Œ 0์ด ๋‚˜์˜จ๋‹ค๋ฉด 9์˜ ๋ฐฐ์ˆ˜๊ฐ€ ๋˜๊ธฐ์— ๊ฒฐ๋ก ์ ์œผ๋กœ 9๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

 

๋” ์ž์„ธํžˆ ์•Œ์•„๋ด…์‹œ๋‹ค. ์•„๋ž˜์˜ ํ‘œ๋Š” 1~100๊นŒ์ง€์˜ digits ์ˆ˜ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค.

1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
---------
10 1
11 2
12 3
13 4
14 5
15 6
16 7
17 8
18 9
--------
19 1
20 2
21 3
22 4
23 5
24 6
25 7
26 8
27 9
-------
28 1
29 2
30 3
31 4
32 5
33 6
34 7
35 8
36 9
-------
37 1
38 2
39 3
40 4
41 5
42 6
43 7
44 8
45 9
------
46 1
47 2
48 3
49 4
50 5
51 6
52 7
53 8
54 9
--------
55 1
56 2
57 3
58 4
59 5
60 6
61 7
62 8
63 9
--------
64 1
65 2
66 3
67 4
68 5
69 6
70 7
71 8
72 9
--------
73 1
74 2
75 3
76 4
77 5
78 6
79 7
80 8
81 9
--------
82 1
83 2
84 3
85 4
86 5
87 6
88 7
89 8
90 9
-------
91 1
92 2
93 3
94 4
95 5
96 6
97 7
98 8
99 9
------
100 1

9๊ฐœ์˜ ์ˆ˜๊ฐ€ ์ฃผ๊ธฐ์ ์œผ๋กœ ๊ฐ’์ด 1~9๊ฐ€ ๋‚˜์˜ด์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. mod 9 ์—ฐ์‚ฐ์„ ํ–‰ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

'Algorithm ๐Ÿง‘๐Ÿปโ€๐Ÿ’ป > Leetcode' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Leetcode,c++] Happy Number  (0) 2022.03.02
[Leetcode,c++] Majority Element  (0) 2021.11.28
[Leetcode,c++] Network Delay Time  (0) 2021.11.22
[Leetcode,c++] Valid Anagram  (0) 2021.11.20
[Leetcode,c++] Single Number  (0) 2021.11.19
[Leetcode,c++] Combination Sum II  (0) 2021.11.16

๋Œ“๊ธ€