leetcode 258 Add Digits(难易度:Easy)
来源:互联网 发布:有个腿长的女朋友知乎 编辑:程序博客网 时间:2024/04/30 16:52
Add Digits
Given a non-negative integer num
, repeatedly add all its digits until the result has only one digit.
For example:
Given num = 38
, the process is like: 3 + 8 = 11
, 1 + 1 = 2
. Since 2
has only one digit, return it.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
Hint:
- A naive implementation of the above process is trivial. Could you come up with other methods?
- What are all the possible results?
- How do they occur, periodically or randomly?
- You may find this Wikipedia article useful.
int addDigits(int num) { if(num >= 0 && num < 10)return num; int sum = 0; while(num) { sum += (num % 10);num /= 10; } return addDigits(sum);}原题地址:https://leetcode.com/problems/add-digits/
0 0
- leetcode 258 Add Digits(难易度:Easy)
- <LeetCode><Easy>258Add Digits
- LeetCode #258 - Add Digits -Easy
- leetcode 258[easy]---Add Digits
- 【leetcode】Add Digits[easy]
- 【Leetcode】Add Digits(easy)
- Leetcode----258.Add Digits(easy)
- LeetCode(258) Add Digits
- leetcode 242 Valid Anagram(难易度:Easy)
- leetcode 058 Length of Last Word(难易度:Easy)
- leetcode 171 Excel Sheet Column Number(难易度:Easy)
- leetcode 136 Single Number(难易度:Easy)
- leetcode 234 Palindrome Linked List(难易度:Easy)
- leetcode 172 Factorial Trailing Zeroes(难易度:Easy)
- leetcode 125 Valid Palindrome(难易度:Easy)
- leetcode 100 Same Tree(难易度:Easy)
- leetcode 191 Number of 1 Bits(难易度:Easy)
- leetcode 226 Invert Binary Tree(难易度:Easy)
- QT---qtextedit中的光标问题
- python 之 推导式
- 慕课学习C++笔记02-封装(上)
- 3-06. 表达式转换(25)
- 几道python笔试题
- leetcode 258 Add Digits(难易度:Easy)
- Java IO流->处理流->打印流:PrintStream&PrintWriter
- Python yield 使用浅析
- Java多线程 -- 有返回值的线程
- Android Camera
- JVM调优总结 -Xms -Xmx -Xmn -Xss
- Redis源码分析---字典dict
- 两道几何题的求解
- 文章标题