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. Makefile中的MAKECMDGOALS

      make 在执行时会设置一个特殊变量 -- "MAKECMDGOALS" ,该变量记录了命令行参数指定的终极目标列表,没有通过参数指定终极目标时此变量为空.该变量仅限于用在特殊 ...

  2. 【大数据系列】windows搭建hadoop开发环境

    一.安装JDK配置环境变量 已经安装略过 二.安装eclipse 已经安装略过 三.安装Ant 1.下载http://ant.apache.org/bindownload.cgi 2.解压 3.配置A ...

  3. VC++ 学习笔记3 获取编辑框字符串

    边界框添加字符串比较简单 可以直接在对话框的空间上面点击右键添加变量,变量类型为CString  在此取名为m_NUM 直接使用m_NUM就是编辑框的CString, 举例: 在messagebox显 ...

  4. CLR 关于强命名程序集 .

    如何创建强命名程序集(Strong Name Assembly)     创建一个强命名程序集首先需要获得一个用强命名实用工具   (Strong Name Utility,即SN.exe,.NET  ...

  5. null类型

    null类型只有一个特殊的值null   null值表示一个空对象指针. var car = null; alert(typeof null);//object   undefined派生自null ...

  6. Thinkphp框架下对某个字段查询数据的时候进行唯一过滤,返回唯一不同的值

    方法一. DISTINCT 方法用于返回唯一不同的值 . *distinct方法的参数是一个布尔值. 用法: $data = $Model->Distinct(true)->field(' ...

  7. VMware 安装CentOS 6.5图文步骤 以及安装后无法联网的解决办法

    一.VMwareWorkstation10 中安装Centos6.5(64位)步骤: 首先下载vmware 和centos6.5 1. 打开VMware-workstation点击“新建虚拟机”,到向 ...

  8. @staticmethod和@classmethod的作用与区别

    一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法. 而使用@staticmethod或@classmethod,就可以不需要实例化,直接类名.方法名()来调用. 这有利于组织代码,把某些应 ...

  9. CodeForce 832A Sasha and Sticks

    A. Sasha and Sticks time limit per test2 seconds memory limit per test256 megabytes inputstandard in ...

  10. HOJ 2985 Wavio Sequence(最长递增子序列以及其O(n*logn)算法)

    Wavio Sequence My Tags (Edit) Source : UVA Time limit : 1 sec Memory limit : 32 M Submitted : 296, A ...