Nav: << previous: 257.二叉树的所有路径 | next: 259.较小的三数之和 >>
Description
tab: English
<p>Given an integer <code>num</code>, repeatedly add all its digits until the result has only one digit, and return it.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num = 38
<strong>Output:</strong> 2
<strong>Explanation:</strong> The process is
38 --> 3 + 8 --> 11
11 --> 1 + 1 --> 2
Since 2 has only one digit, return it.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num = 0
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= num <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you do it without any loop/recursion in <code>O(1)</code> runtime?</p>
> [!tip]- Hint 1
>
> A naive implementation of the above process is trivial. Could you come up with other methods?
> [!tip]- Hint 2
>
> What are all the possible results?
> [!tip]- Hint 3
>
> How do they occur, periodically or randomly?
> [!tip]- Hint 4
>
> You may find this <a href="https://en.wikipedia.org/wiki/Digital_root" target="_blank">Wikipedia article</a> useful.
---
[submissions](https://leetcode.com/problems/add-digits/submissions/) | [solutions](https://leetcode.com/problems/add-digits/solutions/)
tab: 中文
<p>给定一个非负整数 <code>num</code>,反复将各个位上的数字相加,直到结果为一位数。返回这个结果。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong> num =<strong> </strong><code>38</code>
<strong>输出:</strong> 2
<strong>解释: </strong>各位相加的过程为<strong>:
</strong>38 --> 3 + 8 --> 11
11 --> 1 + 1 --> 2
由于 <code>2</code> 是一位数,所以返回 2。
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong> num =<strong> </strong>0
<strong>输出:</strong> 0</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>0 <= num <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<p><strong>进阶:</strong>你可以不使用循环或者递归,在 <code>O(1)</code> 时间复杂度内解决这个问题吗?</p>
> [!tip]- 提示 1
>
> A naive implementation of the above process is trivial. Could you come up with other methods?
> [!tip]- 提示 2
>
> What are all the possible results?
> [!tip]- 提示 3
>
> How do they occur, periodically or randomly?
> [!tip]- 提示 4
>
> You may find this <a href="https://en.wikipedia.org/wiki/Digital_root" target="_blank">Wikipedia article</a> useful.
---
[提交记录](https://leetcode.cn/problems/add-digits/submissions/) | [题解](https://leetcode.cn/problems/add-digits/solution/)
Solutions & Notes
properties:
note.updated:
displayName: Last Updated
note.relative_links:
displayName: Related Links
note.desc:
displayName: Description
note.grade:
displayName: Rating
note.program_language:
displayName: Language
note.time_complexity:
displayName: TC
note.space_complexity:
displayName: SC
views:
- type: table
name: Solutions & Notes
filters:
and:
- file.hasLink(this.file)
- file.tags.containsAny("leetcode/solution", "leetcode/note")
order:
- file.name
- desc
- program_language
- time_complexity
- space_complexity
- grade
- relative_links
- updated
sort:
- property: grade
direction: ASC
- property: time_complexity
direction: ASC
- property: program_language
direction: ASC
columnSize:
file.name: 104
note.space_complexity: 65
note.grade: 126
Similar Problems
properties:
note.lcTopics:
displayName: Topics
note.lcAcRate:
displayName: AC Rate
note.favorites:
displayName: Favorites
note.grade:
displayName: Rating
note.translatedTitle:
displayName: Title (CN)
note.lcDifficulty:
displayName: Difficulty
views:
- type: table
name: Similar Problems
filters:
and:
- file.hasLink(this.file)
- similarQuestions.contains(this.file)
order:
- file.name
- translatedTitle
- lcTopics
- lcDifficulty
- lcAcRate
- grade
- favorites
sort:
- property: file.name
direction: ASC
- property: lcTopics
direction: DESC
columnSize:
note.translatedTitle: 240
note.lcTopics: 347
note.lcAcRate: 75
note.grade: 122