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 ...
随机推荐
- 虚拟机环境中安装ubuntu下的mysql-cluster7.3.2(单点服务器)
部署环境: 系统:ubuntu-12.04.2 LTS -server-i386.iso Cluster:mysql-cluster-gpl-7.3.2-linux-glibc23-i686.ta ...
- ubuntu 16.04 Ubuntu 安装GDebi,从而安装deb文件
其实在ubuntu直接双击deb文件就能安装,可是我现在装了ubuntu 16.04后,发现谷歌浏览器的deb和搜狗输入法的deb都不能直接双击安装,有点小问题. 但是安装GDebi软件后,直接在终端 ...
- js 写table 函数
//创建 table函数 function table(row,col,b,w) { document.write('<table border='+b+'>'); for(var i=0 ...
- ORA-00119: invalid specification for system parameter LOCAL_LISTENER
重启oracle是提示错误ORA-00119: invalid specification for system parameter LOCAL_LISTENER. 解决方法: 命令查看错误信息:oe ...
- Centos 7 python升级(2.7.5-》2.7.11)
1.安装升级GCC yum install gcc* openssl openssl-devel ncurses-devel.x86_64 bzip2-devel sqlite-devel pyth ...
- Android编译过程详解(三)
前面两节讲解了自定义Android编译项和创建Product产品配置文件,除了编译和定义产品相关环境变量外,还需要定义Board相关环境变量. 1. build/core/config.mk 109 ...
- ASP.NET 导出Excel文档
System.IO.TextWriter writer = new System.IO.StreamWriter(Server.MapPath("/provprice.xls"), ...
- Android 多线程断点下载(非原创)
1.服务器的CPU分配给每条线程的时间片相同,服务器带宽平均分配给每条线程,所以客户端开启的线程越多,就能抢占到更多的服务器资源,这里在客户端开启多个线程来从服务器下载资源 2.fragment_ma ...
- .NET的 DataTable中某列求和
public DataTable ReportDetail { get; set; }//定义datatable属性 this.txtTotalPiece.Text = ReportDetail.Co ...
- OC对象:封装、继承、多态的使用举例一
// 该代码在网上找的视频中的例子,感觉很适合类和对象分不清楚的同学参考,仅供学习分享,谢谢 // 创建一个Pointtest类,用属性x.y表示点的坐标位置,求两点之间的距离,使用两种方法:类方法和 ...