Skip to content

Commit 7a7d6a7

Browse files
authored
258. Add Digits (#59)
* 258 solved. * fmt code
1 parent 9487c88 commit 7a7d6a7

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ continually updating 😃.
121121
* [13. Roman to Integer](src/0013_roman_to_integer/roman_to_integer.go)   *`string`*
122122
* [66. Plus One](src/0066_plus_one/plus_one.go)   *`array`*
123123
* [150. Evaluate Reverse Polish Notation](src/0150_evaluate_reverse_polish_notation/evaluate_reverse_polish_notation.go)   *`stack`*
124+
* [258. Add Digits](src/0258_add_digits/add_digits.go)
124125

125126
<details>
126127
</details>

src/0258_add_digits/add_digits.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
258. Add Digits
3+
https://leetcode.com/problems/add-digits/
4+
5+
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
6+
*/
7+
// time: 2019-01-07
8+
9+
package ad
10+
11+
// time complexity: O( log num )
12+
// space complexity: O(1)
13+
func addDigits(num int) int {
14+
// num is a non-negative integer
15+
for num > 9 {
16+
num = performAdd(num)
17+
}
18+
return num
19+
}
20+
21+
func performAdd(num int) (res int) {
22+
for num > 0 {
23+
res += num % 10
24+
num /= 10
25+
}
26+
return
27+
}
28+
29+
// time complexity: O(1)
30+
// space complexity: O(1)
31+
func addDigits1(num int) int {
32+
/*
33+
0 1 2 3 4 5 6 7 8 9
34+
0 1 2 3 4 5 6 7 8 9
35+
10 11 12 13 14 15 16 17 18 19
36+
1 2 3 4 5 6 7 8 9 10/1
37+
20 21 22 23 24 25 26 27 28 29
38+
2 3 4 5 6 7 8 9 10/1 11/2
39+
*/
40+
return (num-1)%9 + 1
41+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package ad
2+
3+
import "testing"
4+
5+
func TestAddDigits(t *testing.T) {
6+
num := 38
7+
expected := 2
8+
9+
testFuncs := []func(int) int{
10+
addDigits,
11+
addDigits1,
12+
}
13+
14+
for _, function := range testFuncs {
15+
if res := function(num); res != expected {
16+
t.Errorf("expected %d, got %d", expected, res)
17+
}
18+
}
19+
}

src/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
|0235|[235. Lowest Common Ancestor of a Binary Search Tree](0235_lowest_common_ancestor_of_a_binary_search_tree/lcaoabst.go)|Easy|*`recursion; `* *`binary tree`*|
7575
|0237|[237. Delete Node in a Linked List](0237_delete_node_in_a_linked_list/dniall.go)|Easy|*`linked list`*|
7676
|0257|[257. Binary Tree Paths](0257_binary_tree_paths/binary_tree_paths.go)|Easy|*`binary tree`*|
77+
|0258|[258. Add Digits](0258_add_digits/add_digits.go)|Easy|*`math`*|
7778
|0283|[Move Zeroes(solution1)](./0283_move_zeroes/move_zeroes.go) <br/> [Move Zeroes(solution2)](./0283_move_zeroes/move_zeroes2.go)|Easy|*`array`*|
7879
|0300|[Longest Increasing Subsequence](./0300_longest_increasing_subsequence/lis.go)|Medium|*`dp`*|
7980
|0303|[303. Range Sum Query - Immutable](0303_range_sum_query/rsqim.go)|Easy||

0 commit comments

Comments
 (0)