题目:

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. Swift初步介绍

    Swift是本届WWDC大会苹果推出的一门新开发语言,开发者网站上已经放出了这门新语言的介绍.教程和手册,如果手里有一台iOS设备的话,通过苹果的iBooks应用,从它的官方书店里搜索Swift,可以 ...

  2. Python开发【第一篇】Python基础之正则表达式补充

    正则表达式 一简介:就其本质而言,正则表达式(或RE)是一种小型的.高度专业化的标称语言,(在Python中)它内嵌在Python中,并通过re模块实现.正则表达式模式被编译成一系列的字节码,然后由用 ...

  3. 一个简单WPF登陆界面,包含记住密码,自动登录等功能,简洁美观

    简介:这是一个自己以前用WPF设计的登陆界面,属于一个实验性的界面窗体,如果用于产品还很有不足.但也是有一点学习价值.后台代码略有复杂,但基本上都有注释 分类,略有代码经验的一般都能看懂. 登陆界面外 ...

  4. XAML 概述二

    通过上一节我们已经对XAML有了一定的了解,这一节我们来系统的学习一下XAML. 一. 简单属性与类型转换器,属性元素: 我们已经知道 XAML是一种声明性的语言,并且XAML解析器会为每个标签创建一 ...

  5. R语言基础(一) 可视化基础

    ##数据获取 x1=round(runif(100,min=80,max=100)) x2=round(rnorm(100,mean=80, sd=7)) x3=round(rnorm(100,mea ...

  6. 【nodejs】 文件系统(fs) 之读写文件

    //写入文件 var data = "hello world"; fs.writeFile('c:\\a.txt', data, 'ascii', function(err) { ...

  7. vxworks启动

  8. Spiral Matrix

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  9. Unity3D 利用NGUI2.6.3做技能冷却的CD效果

    转自http://blog.csdn.net/qqmcy/article/details/9469021 NGUI非常强大我们今天来学习一下,如何利用NGUI做技能冷却的CD效果.先导入NGUI的插件 ...

  10. UAP如何根据DeviceFamily显示不同的页面

    背景 微软推出UAP 推荐使用响应式的UI,但是难免遇到一些特殊情况需要使用不同的Page来在不同的设备显示. 微软目前最新的VS2015在10074上安装后能够支持这个功能特性,只是暂时没有文档介绍 ...