题目:

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 = 111 + 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:

    1. A naive implementation of the above process is trivial. Could you come up with other methods?
    2. What are all the possible results?
    3. How do they occur, periodically or randomly?
    4. 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的更多相关文章

  1. 258. Add Digits(C++)

    258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has ...

  2. 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 ...

  3. LN : leetcode 258 Add Digits

    lc 258 Add Digits lc 258 Add Digits Given a non-negative integer num, repeatedly add all its digits ...

  4. 【LeetCode】258. Add Digits (2 solutions)

    Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...

  5. LeetCode 258. Add Digits

    Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...

  6. (easy)LeetCode 258.Add Digits

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  7. Java [Leetcode 258]Add Digits

    题目描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...

  8. 【LeetCode】258. Add Digits

    题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...

  9. 【一天一道LeetCode】#258. Add Digits

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. WPF-控件-将ListBox条目水平排列

    <Grid Margin="6"> <ListBox> <!--ItemsPanel--> <ListBox.ItemsPanel> ...

  2. iOS中Cell高度如何能够自动适应需要显示的内容

    本文的代码例子 : "Cell行高自适应.zip" http://vdisk.weibo.com/s/Gb9Mt 下面我们来看看代码.我需要一个第三方库EGO异步下载.addtio ...

  3. iOS 进阶 第十一天(0411)

    0411 UItaBbar的结构 每一个数组都有一个方法,那就是下面这个,如下图所示: 如果想看系统控件是怎么构成的,那么就采用遍历其子控件的方式来做,如上一图中所示 在iOS7及其以后的系统里,控制 ...

  4. 第三章 DispatcherServlet详解

    3.1.DispatcherServlet作用 DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点,而且负责职责的分派,而且与Spring Io ...

  5. 微软职位内部推荐-SDE2 (Windows - Audio)

    微软近期Open的职位: SDE2 (Windows - Audio) Windows Partner Enablement team in Operating System Group is loo ...

  6. spring配置事务

    一.配置JDBC事务处理机制 <!-- 配置Hibernate事务处理 --> <bean id="transactionManager" class=" ...

  7. Win7超级终端查看单片机printf输出

    问题描述:     编写单片机C程序时,经常会用到printf输出信息进行查看,如何查看printf输出? 问题解决:     (1)编写单片机C程序     ucos是一个实时多任务操作系统,以上是 ...

  8. Matlab计算两集合间的海明距离

    一.问题描述 B1[1 2 3 4 5 6 7 8 9] B2[12 13 14 21 31 41 51  1 1 81 1 1] 两个十进制矩阵,行数不一样,分别是n1和n2,列数必须一致,为nwo ...

  9. State of Hyperparameter Selection

    State of Hyperparameter Selection DANIEL SALTIEL VIEW NOTEBOOK Historically hyperparameter determina ...

  10. [设计模式] 7 桥接模式 bridge

    #include<iostream> using namespace std; class AbstractionImp { public: virtual ~AbstractionImp ...