Skip to content

Commit 53ac336

Browse files
refactor 258
1 parent 4c9e27a commit 53ac336

File tree

1 file changed

+10
-27
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+10
-27
lines changed
Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,17 @@
11
package com.fishercoder.solutions;
22

3-
/**
4-
* Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
5-
6-
For example:
7-
8-
Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
9-
10-
Follow up:
11-
Could you do it without any loop/recursion in O(1) runtime?
12-
13-
Hint:
14-
15-
A naive implementation of the above process is trivial. Could you come up with other methods?
16-
What are all the possible results?
17-
How do they occur, periodically or randomly?
18-
You may find this Wikipedia article: https://en.wikipedia.org/wiki/Digital_root useful.
19-
* */
20-
213
public class _258 {
224

23-
//only three cases as the code shows
24-
public int addDigits(int num) {
25-
if (num == 0) {
26-
return 0;
5+
public static class Solution1 {
6+
//only three cases as the code shows
7+
public int addDigits(int num) {
8+
if (num == 0) {
9+
return 0;
10+
}
11+
if (num % 9 == 0) {
12+
return 9;
13+
}
14+
return num % 9;
2715
}
28-
if (num % 9 == 0) {
29-
return 9;
30-
}
31-
return num % 9;
3216
}
33-
3417
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy