Add Digits 解答
Question
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.
Solution
Naive way is to use two loops.
public class Solution {
public int addDigits(int num) {
int copy = num;
while (copy / 10 > 0) {
int newNum = 0;
while (copy / 10 > 0) {
newNum += copy % 10;
copy /= 10;
}
newNum += copy;
copy = newNum;
}
return copy;
}
}
There is a math relationship.


public class Solution {
public int addDigits(int num) {
if (num == 0)
return num;
if (num % 9 == 0)
return 9;
return (num % 9);
}
}
Add Digits 解答的更多相关文章
- 【LeetCode】Add Digits
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference
最近做的题记录下. 258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the ...
- 【LeetCode】258. Add Digits (2 solutions)
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- [LeetCode] Add Digits (a New question added)
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- 258. Add Digits(C++)
258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has ...
- LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree
258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...
- 【02_258】Add Digits
Add Digits Total Accepted: 49702 Total Submissions: 104483 Difficulty: Easy Given a non-negative int ...
- LeetCode:Add Digits - 非负整数各位相加
1.题目名称 Add Digits (非负整数各位相加) 2.题目地址 https://leetcode.com/problems/add-digits/ 3.题目内容 英文:Given a non- ...
- LN : leetcode 258 Add Digits
lc 258 Add Digits lc 258 Add Digits Given a non-negative integer num, repeatedly add all its digits ...
随机推荐
- 第02讲- Android开发环境
第02讲Android开发环境 需要下载的软件: JDK(JavaDevelopment Kit) Eclipse AndroidSDK(SoftwareDevelopmentKit) ADT(And ...
- IOS 判断设备类型
- (NSString*)deviceString { // 需要#import "sys/utsname.h" struct utsname systemInfo; uname( ...
- PC--CSS优化
优化你的CSS 所谓高效的CSS就是让浏览器在查找style匹配的元素的时候尽量进行少的查找,下面列出一些我们常见的写CSS犯一些低效错误: 1.不要在ID选择器前使用标签名一般写法:DIV#divB ...
- Vlc for Android 全面阐述
简单介绍 Vlc for android是一款开源安卓播放器.具备播放多媒体文件.光盘.设备以及网络流媒体协议等功能,支持ARMv7 CPU或一个x86 CPU的设备,全部播放控制特性都已经开发完整. ...
- 【解决方法】EasyUI DataGrid不显示滚动条时,没有数据的问题
解决方法 于dataGrid例如,下面的代码被添加到的定义: JavaScript Code 1 2 3 4 5 6 7 8 9 10 onLoadSuccess : function (data ...
- boost库在工作(39)网络UDP异步服务端之九
前面创建的UDP服务器和客户端,都是同步的方式,也就是说当接收数据时,不能参与别的事情执行的.如果在一个只有界面线程的程序里,又不想创建多线程,导致复杂程度的增加,在这种情况之下,我们还有一个方案可以 ...
- Apache与tomcat
联系 1)Apache和tomcat都是web网络服务器 2)Apache是普通的服务器,本身支持html即普通网页,可以通过插件支持php也可以与Tomcat连通 (Apache单向连接tomca ...
- TreeView中右击直接获取节点的方法
在TreeView中无法直接右击得到一个节点,因为当你选中其中一个右击时(不能是第一个)他会默认跳到第一个. 有时我们要想直接右击得到选中的节点,又时我们又想选中直接右击跳出一个快捷菜单怎么办了! 在 ...
- iOS_block内存分析
----------------------MRC情况下Block内存分析---------------------------- 1.如果在block中使用全局变量,他为了持有这个变量,会将对应的对 ...
- poj 3158kickdown
我是来吐槽poj的!!! 第一次做poj,被题目中的输入输出格式打败了 ,醉了醉了 Description A research laboratory of a world-leading autom ...