Home LeetCode 258. Add Digits
Post
Cancel

LeetCode 258. Add Digits

258. Add Digits

Problem

1
2
Given an integer num, repeatedly add all its digits until the result has only one digit, and return it.

Questions before reading example

Example

1
2
3
4
5
6
Input: num = 38
Output: 2
Explanation: The process is
38 --> 3 + 8 --> 11
11 --> 1 + 1 --> 2
Since 2 has only one digit, return it.

Solution

  • 나의 풀이
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
  public int addDigits(int num) {
    String numString = String.valueOf(num);

    while (numString.length() > 1) {
      int sum = 0;
      for (char c : numString.toCharArray()) {
        sum += c - '0';
      }
      numString = String.valueOf(sum);
    }

    return Integer.parseInt(numString);
  }
}
  • 재귀로 푼 풀이
    1
    2
    3
    4
    5
    6
    7
    8
    
    class Solution {
    
    public int sumDigits(int n) {
      if (n == 0)
        return 0;
      return (n % 10) + sumDigits(n / 10);
    }
    }
    

    Spent time

  • 5m

Review

  • HappyNumber 와 같은 방법으로 풀었다.
  • 재귀로도 문제를 깔끔하게 풀 수 있는데.. 이 방법에 더 익숙해지고 싶다.
This post is licensed under CC BY 4.0 by the author.