Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a167b5b

Browse files
committed
commit solution 258
1 parent f85b5a7 commit a167b5b

File tree

6 files changed

+90
-8
lines changed

6 files changed

+90
-8
lines changed

‎index-tags.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
| [171](https://leetcode-cn.com/problems/excel-sheet-column-number) | [excel表列序号](/solution/100-199/0171.excel-sheet-column-number/) | `数学` | <font color=green>简单</font> ||
5757
| [202](https://leetcode-cn.com/problems/happy-number) | [快乐数](/solution/200-299/0202.happy-number/) | `哈希表`,`数学` | <font color=green>简单</font> ||
5858
| [204](https://leetcode-cn.com/problems/count-primes) | [计数质数](/solution/200-299/0204.count-primes/) | `哈希表`,`数学` | <font color=green>简单</font> ||
59+
| [258](https://leetcode-cn.com/problems/add-digits) | [各位相加](/solution/200-299/0258.add-digits/) | `数学` | <font color=green>简单</font> ||
5960
| [268](https://leetcode-cn.com/problems/missing-number) | [缺失数字](/solution/200-299/0268.missing-number/) | `位运算`,`数组`,`数学` | <font color=green>简单</font> ||
6061

6162
#### **哈希表**
@@ -72,6 +73,7 @@
7273
| [205](https://leetcode-cn.com/problems/isomorphic-strings) | [同构字符串](/solution/200-299/0205.isomorphic-strings/) | `哈希表` | <font color=green>简单</font> ||
7374
| [217](https://leetcode-cn.com/problems/contains-duplicate) | [存在重复元素](/solution/200-299/0217.contains-duplicate/) | `数组`,`哈希表` | <font color=green>简单</font> ||
7475
| [219](https://leetcode-cn.com/problems/contains-duplicate-ii) | [存在重复元素 ii](/solution/200-299/0219.contains-duplicate-ii/) | `数组`,`哈希表` | <font color=green>简单</font> ||
76+
| [290](https://leetcode-cn.com/problems/word-pattern) | [单词规律](/solution/200-299/0290.word-pattern/) | `哈希表` | <font color=green>简单</font> ||
7577

7678
#### ****
7779

@@ -120,6 +122,7 @@
120122
| [69](https://leetcode-cn.com/problems/sqrtx) | [x 的平方根](/solution/1-99/0069.sqrt%28x%29/) | `数学`,`二分查找` | <font color=green>简单</font> ||
121123
| [167](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted) | [两数之和 ii - 输入有序数组](/solution/100-199/0167.two-sum-ii---input-array-is-sorted/) | `数组`,`双指针`,`二分查找` | <font color=green>简单</font> ||
122124
| [209](https://leetcode-cn.com/problems/minimum-size-subarray-sum) | [长度最小的子数组](/solution/200-299/0209.minimum-size-subarray-sum/) | `数组`,`双指针`,`二分查找` | <font color=blue>中等</font> ||
125+
| [278](https://leetcode-cn.com/problems/first-bad-version) | [第一个错误的版本](/solution/200-299/0278.first-bad-version/) | `二分查找` | <font color=green>简单</font> ||
123126

124127
#### **排序**
125128

@@ -177,4 +180,7 @@
177180

178181
#### **脑筋急转弯**
179182

183+
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
184+
| --- | --- | --- | --- | --- |
185+
| [292](https://leetcode-cn.com/problems/nim-game) | [nim 游戏](/solution/200-299/0292.nim-game/) | `脑筋急转弯`,`极小化极大` | <font color=green>简单</font> ||
180186
<!-- tabs:end -->

‎index-type.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,16 @@
8484
| [252](https://leetcode-cn.com/problems/meeting-rooms) | [会议室](/solution/200-299/0252.meeting-rooms/) | `排序` | <font color=green>简单</font> |
8585
| [256](https://leetcode-cn.com/problems/paint-house) | [粉刷房子](/solution/200-299/0256.paint-house/) | `动态规划` | <font color=green>简单</font> |
8686
| [257](https://leetcode-cn.com/problems/binary-tree-paths) | [二叉树的所有路径](/solution/200-299/0257.binary-tree-paths/) | ``,`深度优先搜索` | <font color=green>简单</font> |
87-
| [258](https://leetcode-cn.com/problems/add-digits) | [各位相加](/solution/200-299/0258.add-digits/) | `数学` | <font color=green>简单</font> |
87+
| [258](https://leetcode-cn.com/problems/add-digits) | [各位相加](/solution/200-299/0258.add-digits/) | `数学` | <font color=green>简单</font> ||
8888
| [263](https://leetcode-cn.com/problems/ugly-number) | [丑数](/solution/200-299/0263.ugly-number/) | `数学` | <font color=green>简单</font> |
8989
| [266](https://leetcode-cn.com/problems/palindrome-permutation) | [回文排列](/solution/200-299/0266.palindrome-permutation/) | `哈希表` | <font color=green>简单</font> |
9090
| [268](https://leetcode-cn.com/problems/missing-number) | [缺失数字](/solution/200-299/0268.missing-number/) | `位运算`,`数组`,`数学` | <font color=green>简单</font> ||
9191
| [270](https://leetcode-cn.com/problems/closest-binary-search-tree-value) | [最接近的二叉搜索树值](/solution/200-299/0270.closest-binary-search-tree-value/) | ``,`二分查找` | <font color=green>简单</font> |
9292
| [276](https://leetcode-cn.com/problems/paint-fence) | [栅栏涂色](/solution/200-299/0276.paint-fence/) | `动态规划` | <font color=green>简单</font> |
93-
| [278](https://leetcode-cn.com/problems/first-bad-version) | [第一个错误的版本](/solution/200-299/0278.first-bad-version/) | `二分查找` | <font color=green>简单</font> |
93+
| [278](https://leetcode-cn.com/problems/first-bad-version) | [第一个错误的版本](/solution/200-299/0278.first-bad-version/) | `二分查找` | <font color=green>简单</font> ||
9494
| [283](https://leetcode-cn.com/problems/move-zeroes) | [移动零](/solution/200-299/0283.move-zeroes/) | `数组`,`双指针` | <font color=green>简单</font> ||
95-
| [290](https://leetcode-cn.com/problems/word-pattern) | [单词规律](/solution/200-299/0290.word-pattern/) | `哈希表` | <font color=green>简单</font> |
96-
| [292](https://leetcode-cn.com/problems/nim-game) | [nim 游戏](/solution/200-299/0292.nim-game/) | `脑筋急转弯`,`极小化极大` | <font color=green>简单</font> |
95+
| [290](https://leetcode-cn.com/problems/word-pattern) | [单词规律](/solution/200-299/0290.word-pattern/) | `哈希表` | <font color=green>简单</font> ||
96+
| [292](https://leetcode-cn.com/problems/nim-game) | [nim 游戏](/solution/200-299/0292.nim-game/) | `脑筋急转弯`,`极小化极大` | <font color=green>简单</font> ||
9797
| [293](https://leetcode-cn.com/problems/flip-game) | [翻转游戏](/solution/200-299/0293.flip-game/) | `字符串` | <font color=green>简单</font> |
9898
| [299](https://leetcode-cn.com/problems/bulls-and-cows) | [猜数字游戏](/solution/200-299/0299.bulls-and-cows/) | `哈希表` | <font color=green>简单</font> |
9999
| [303](https://leetcode-cn.com/problems/range-sum-query-immutable) | [区域和检索 - 数组不可变](/solution/300-399/0303.range-sum-query---immutable/) | `动态规划` | <font color=green>简单</font> |
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# [258. 各位相加](https://leetcode-cn.com/problems/add-digits/description/)
2+
3+
### 题目描述
4+
5+
<p>给定一个非负整数 <code>num</code>,反复将各个位上的数字相加,直到结果为一位数。</p>
6+
7+
<p><strong>示例:</strong></p>
8+
9+
<pre><strong>输入:</strong> <code>38</code>
10+
<strong>输出:</strong> 2
11+
<strong>解释: </strong>各位相加的过程为<strong>:</strong><code>3 + 8 = 11</code>, <code>1 + 1 = 2</code>。 由于&nbsp;<code>2</code> 是一位数,所以返回 2。
12+
</pre>
13+
14+
<p><strong>进阶:</strong><br>
15+
你可以不使用循环或者递归,且在 O(1) 时间复杂度内解决这个问题吗?</p>
16+
17+
### 解题思路
18+
19+
20+
### 具体解法
21+
22+
23+
#### **Golang**
24+
```go
25+
func addDigits(num int) int {
26+
if num < 10 {
27+
return num
28+
}
29+
sum := 0
30+
for num != 0 {
31+
sum += num % 10
32+
num = num / 10
33+
}
34+
return addDigits(sum)
35+
}
36+
```
37+
38+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=258 lang=golang
5+
*
6+
* [258] 各位相加
7+
*/
8+
9+
// @lc code=start
10+
func addDigits(num int) int {
11+
if num < 10 {
12+
return num
13+
}
14+
sum := 0
15+
for num != 0 {
16+
sum += num % 10
17+
num = num / 10
18+
}
19+
return addDigits(sum)
20+
}
21+
22+
// @lc code=end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestAddDigits(t *testing.T) {
8+
var ret int
9+
var num int
10+
11+
num = 38
12+
ret = 2
13+
if ret != addDigits(num) {
14+
t.Fatalf("case fails %v\n", ret)
15+
}
16+
}

‎solution/200-299/_sidebar.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
- [255. 验证前序遍历序列二叉搜索树](solution/200-299/0255.verify-preorder-sequence-in-binary-search-tree/)
6565
- [256. 粉刷房子](solution/200-299/0256.paint-house/)
6666
- [257. 二叉树的所有路径](solution/200-299/0257.binary-tree-paths/)
67-
- [258. 各位相加](solution/200-299/0258.add-digits/)
67+
- [258. 各位相加](solution/200-299/0258.add-digits/)
6868
- [259. 较小的三数之和](solution/200-299/0259.3sum-smaller/)
6969
- [260. 只出现一次的数字 iii](solution/200-299/0260.single-number-iii/)
7070
- [261. 以图判树](solution/200-299/0261.graph-valid-tree/)
@@ -84,7 +84,7 @@
8484
- [275. h指数 ii](solution/200-299/0275.h-index-ii/)
8585
- [276. 栅栏涂色](solution/200-299/0276.paint-fence/)
8686
- [277. 搜寻名人](solution/200-299/0277.find-the-celebrity/)
87-
- [278. 第一个错误的版本](solution/200-299/0278.first-bad-version/)
87+
- [278. 第一个错误的版本](solution/200-299/0278.first-bad-version/)
8888
- [279. 完全平方数](solution/200-299/0279.perfect-squares/)
8989
- [280. 摆动排序](solution/200-299/0280.wiggle-sort/)
9090
- [281. 锯齿迭代器](solution/200-299/0281.zigzag-iterator/)
@@ -96,9 +96,9 @@
9696
- [287. 寻找重复数](solution/200-299/0287.find-the-duplicate-number/)
9797
- [288. 单词的唯一缩写](solution/200-299/0288.unique-word-abbreviation/)
9898
- [289. 生命游戏](solution/200-299/0289.game-of-life/)
99-
- [290. 单词规律](solution/200-299/0290.word-pattern/)
99+
- [290. 单词规律](solution/200-299/0290.word-pattern/)
100100
- [291. 单词规律 ii](solution/200-299/0291.word-pattern-ii/)
101-
- [292. nim 游戏](solution/200-299/0292.nim-game/)
101+
- [292. nim 游戏](solution/200-299/0292.nim-game/)
102102
- [293. 翻转游戏](solution/200-299/0293.flip-game/)
103103
- [294. 翻转游戏 ii](solution/200-299/0294.flip-game-ii/)
104104
- [295. 数据流的中位数](solution/200-299/0295.find-median-from-data-stream/)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /