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

Example:

Input: 38
Output: 2
Explanation: 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?

基本方法, 用一个helper function, 去计算num的digits sum, 然后如果不是< 10, 继续循环call helper function.

Math 方法: O(1)

The problem, widely known as digit root problem, has a congruence formula:

https://en.wikipedia.org/wiki/Digital_root#Congruence_formula

For base b (decimal case b = 10), the digit root of an integer is:

  • dr(n) = 0 if n == 0
  • dr(n) = (b-1) if n != 0 and n % (b-1) == 0
  • dr(n) = n mod (b-1) if n % (b-1) != 0

or

  • dr(n) = 1 + (n - 1) % 9

Note here, when n = 0, since (n - 1) % 9 = -1, the return value is zero (correct).



Code

1) while

class Solution:
def addDigits(self, num):
"""
:type num: int
:rtype: int
"""
def helper(num):
return sum(int(c) for c in str(num))
while num > :
num = helper(num)
return num

2) Math

class Solution:
def addDigits(self, num):
if num == 0: return 0
return 1 + (num-1)%9

[LeetCode] 258. Add Digits_Easy tag: Math的更多相关文章

  1. [LeetCode] 415. Add Strings_Easy tag: String

    Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...

  2. LN : leetcode 258 Add Digits

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

  3. LeetCode 616. Add Bold Tag in String

    原题链接在这里:https://leetcode.com/problems/add-bold-tag-in-string/description/ 题目: Given a string s and a ...

  4. LeetCode 258 Add Digits(数字相加,数字根)

    翻译 给定一个非负整型数字,反复相加其全部的数字直到最后的结果仅仅有一位数. 比如: 给定sum = 38,这个过程就像是:3 + 8 = 11.1 + 1 = 2.由于2仅仅有一位数.所以返回它. ...

  5. [LeetCode] 258. Add Digits 加数字

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

  6. LeetCode 258. Add Digits

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

  7. (easy)LeetCode 258.Add Digits

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

  8. Java [Leetcode 258]Add Digits

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

  9. LeetCode 258 Add Digits 解题报告

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

随机推荐

  1. Esper学习之九:EPL语法(五)

    本篇的内容主要包括了Subquery(也就是子查询)和Join,内容不少,但是不难,基本上和sql差不太多. 1.Subquery EPL里的Subquery和sql的类似,是否比sql的用法更多我不 ...

  2. MySQL数据库执行sql语句创建数据库和表提示The 'InnoDB' feature is disabled; you need MySQL built with 'InnoDB' to have it working

    MySQL创建数据库 只想sql文件创建表时候提示 The 'InnoDB' feature is disabled; you need MySQL built with 'InnoDB' to ha ...

  3. Android Log缓冲区大小设置

    当手机没有连接PC时,手机log缓冲区仍然会保存指定大小的最新log,连接pc,通过adb logcat 仍然可以拿出来 如何查看log缓缓区的大小? 通过adb logcat -g 可以查看 C:\ ...

  4. 题目1010:A + B(字符串转数字)

    题目链接:http://ac.jobdu.com/problem.php?pid=1010 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  5. Linux SVN 服务器

    一. SVN 简介 Subversion(SVN) 是一个开源的版本控制系統, 也就是说 Subversion 管理着随时间改变的数据. 这些数据放置在一个中央资料档案库 (repository) 中 ...

  6. POJ 2386 Lake Counting(搜索联通块)

    Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48370 Accepted: 23775 Descr ...

  7. 爬虫之requests详解

    requests Python标准库中提供了:urllib.urllib2.httplib等模块以供Http请求,但是,它的 API 太渣了.它是为另一个时代.另一个互联网所创建的.它需要巨量的工作, ...

  8. 如何使ul中li元素横向排列且不换行

    外层div容器宽度固定,ul宽度随li(li宽度固定)的增加而撑开,但是当ul中li的宽度之和大于div时,ul没有撑开,而是li换行了,如何使li不换行? 解决方法:主要是外面容器设置为white- ...

  9. 源码 <oss2.models.PutObjectResult object at 0x7f7928d0b240>

    <oss2.models.PutObjectResult object at 0x7f7928d0b240> # -*- coding: utf-8 -*- import oss2 oss ...

  10. JS-nodejs--nodewebkit--npm穿墙cnpm

    npm install 本地安装与全局安装的区别 本地安装1. 将安装包放在 ./node_modules 下(运行npm时所在的目录)2. 可以通过 require() 来引入本地安装的包 全局安装 ...