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.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
挺简单的一道题。
public

给定一个非负整数num,不断将所有位上的数字相加,直到结果只剩一位。例如,对于num=38,过程为3+8=11,1+1=2。由于2只有一个数字,返回它。挑战是在O(1)的时间复杂度内不使用循环或递归实现。问题启发思考:是否存在其他解决方案?所有可能的结果是什么?它们出现的规律是周期性的还是随机的?
订阅专栏 解锁全文
390

被折叠的 条评论
为什么被折叠?



