258. 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.
链接: http://leetcode.com/problems/add-digits/
题解:
又是数学题,求digital root。循环叠加比较容易,但看了wiki以后发现了公式,还是用公式算吧。这种数学题对数学不好的我来说真是头大。原理10 % 9 或者 100 % 9都等于 1 % 9。举个例子n = abc = a * 100 + b * 10 + c,那么 (a*100 + b * 10 + c) % 9 = (a + b + c) % 9。由此n == 0时,result = 0, n % 9 == 0时, 说明a + b + c = 9,我们返回9,对于其他数字, (a + b + c)等于res % 9。
Time Complexity - O(1), Space Complexity - O(1)
public class Solution {
public int addDigits(int num) {
return 1 + (num - 1) % 9;
}
}
二刷:
Java:
public class Solution {
public int addDigits(int num) {
return 1 + (num - 1) % 9;
}
}
三刷:
发现前两刷其实并没有完全理解,也许就是看了discuss区的答案而已。为什么(a + b + c) mod 9 = (abc) mod 9, 真正用到的公式是modulo运算的分配和结合律。
1. (a + b) mod n = ((a mod n) + (b mod n)) mod n
2. (a * b) mod n = ((a mod n) * (b mod n)) mod n
假如一个数字的三位字符是abc,那么这个数等于 a * 100 + b * 10 + c, 根据分配律, (a * 100) mod 9 = ((a mod 9) * (100 mod 9)) mod 9 = a mod 9,b和c同理, 所以 (a * 100 + b * 10 + c) mod 9 = (a + b + c) mod 9。 我们还可以使用一个小技巧,再用一次分配律直接用 (num - 1) mod 9 + 1来得到结果,这样可以避免一些边界条件的判断。
public class Solution {
public int addDigits(int num) {
if (num == 0) {
return 0;
}
int res = num % 9;
return res == 0 ? 9 : res;
}
}
public class Solution {
public int addDigits(int num) {
return 1 + (num - 1) % 9;
}
}
Update:
public class Solution {
public int addDigits(int num) {
if (num <= 0) return 0;
return (num % 9 == 0) ? 9 : num % 9;
}
}
Reference:
https://en.wikipedia.org/wiki/Digital_root
https://en.wikipedia.org/wiki/Modulo_operation
https://leetcode.com/discuss/67755/3-methods-for-python-with-explains
https://leetcode.com/discuss/52122/accepted-time-space-line-solution-with-detail-explanations
https://leetcode.com/discuss/55910/two-lines-c-code-with-explanation
258. Add Digits的更多相关文章
- 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 ...
- LN : leetcode 258 Add Digits
lc 258 Add Digits lc 258 Add Digits Given a non-negative integer num, repeatedly add all its digits ...
- 【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 258. Add Digits
Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...
- (easy)LeetCode 258.Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- Java [Leetcode 258]Add Digits
题目描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...
- 【LeetCode】258. Add Digits
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
- 【一天一道LeetCode】#258. Add Digits
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- 说Win7激活
今天晚上给电脑来了个强制关机,后来打开后提示我,该Windos不是正版,顿时无语.诺,看下图:我的桌面也全部变成黑色了…… 后来一想……哦,应该是我的安装光盘里的激活工具激活的不彻底,或者说只是给我激 ...
- OpenNMS界面图 .
http://demo.opennms.org/opennms/login.jsp;jsessionid=zibykal1cw4b1cir8wgn0a8b0 这个是opennms的demo websi ...
- AJAX异步同步
为了更好的用户体验,AJAX的异步同步技术给了我们一个很好的用户体验下面是我做的一个例子. 1.客户端处理 UserId.HTML <!DOCTYPE html PUBLIC "-// ...
- Andriod wifi 基本操作
从用户角度看,Android Wi-Fi模块自下向上可以看为5层:硬件驱动程序,wpa_suppplicant,JNI,WiFi API,WifiSettings应用程序. 1.wpa_supplic ...
- C#的winform拼数字游戏
C#的winform拼数字游戏 声明:阅读了别人的代码学习修改而来,增加了美观度和游戏乐趣.(作者出处忘了不好意思) 程序截图 关键代码 using System; using System.Coll ...
- windows下SSH客户端远程访问Linux出现错误
- Sublime Text 2 入门
SublimeText 2 的介绍视频: http://player.youku.com/player.php/partnerid/XOTcy/sid/XMzU5NzQ5ODgw/v.swf 以下 ...
- Windows Media Player安装了却不能播放网页上的视频
前段时间遇到Windows Media Player安装了却不能播放网页上的视频的问题,在网上查找资料时,发现大部分资料都没能解决我这个问题.偶尔试了网上一牛人的方法,后来竟然解决了.现在再找那个网页 ...
- html5游戏引擎-Pharse.js学习笔记(一)
1.前言 前几天随着flappy bird这样的小游戏的火爆,使我这种也曾了解过html5技术的js业余爱好者也开始关注游戏开发.研究过两个个比较成熟的html5游戏引擎,感觉用引擎还是要方便一些.所 ...
- 不同的source control下配置DiffMerge
TFS: 1. 打开Option -> Source Control -> Visual Studio TFS -> Configure User Tools; 2. 添加 .*, ...