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?

解法1:

class Solution(object):
def addDigits(self, num):
"""
:type num: int
:rtype: int
"""
# 1-9=1-9
# 10=1
# 11=2
# 12=3 ...
# 18=9
# 19=>1
# 20=>2
# 21=>3
# 99=>9
# 100=>1
# 101=>2
# 999=>9
def sum_digits(n):
ans = 0
while n:
ans += n%10
n /= 10
return ans ans = num
while ans > 9:
ans = sum_digits(ans)
return ans

观察发现是一个循环数组:

class Solution(object):
def addDigits(self, num):
"""
:type num: int
:rtype: int
"""
# 1-9=1-9
# 10=1
# 11=2
# 12=3 ...
# 18=9
# 19=>1
# 20=>2
# 21=>3
# 99=>9
# 100=>1
# 101=>2
# 999=>9
if num == 0: return 0
return 9 if num % 9 == 0 else num % 9

leetcode 258. Add Digits——我擦,这种要你O(1)时间搞定的必然是观察规律,总结一个公式哇的更多相关文章

  1. LN : leetcode 258 Add Digits

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

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

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

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

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

  4. LeetCode 258. Add Digits

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

  5. (easy)LeetCode 258.Add Digits

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

  6. Java [Leetcode 258]Add Digits

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

  7. LeetCode 258 Add Digits 解题报告

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

  8. leetcode 258. Add Digits(数论)

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

  9. LeetCode: 258 Add Digits(easy)

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

随机推荐

  1. 查看密码存放地-shadow

    shadow 位置:/cat/shadow 作用:存放用户的密码等信息 使用查看命令以后得到以下数据 我们会看到9个字段,分别用  :隔开,如上图所示一一解释: 第一字段:用户名称 第二字段:加密密码 ...

  2. delphi clipboard

    判断clipboard里的格式: if   CliPBoard.HasFormat(CF_TEXT)   then        EdIT1.Text   :=   CliPBoard.AsText ...

  3. python3.x Day6 多线程

    线程???进程????区别???何时使用??? 进程:是程序以一个整体的形式暴露给操作系统管理,里边包含了对各种资源的调用,内存的使用,对各种资源的管理的集合,这就叫进程 线程:是操作系统最小的调度单 ...

  4. list & dictionary

    list不能直接进行对应,dictionary可以 list用[],dictionary用{}

  5. super在python中有什么用

    所属网站分类: python高级 > 面向对象 作者:阿里妈妈 链接:http://www.pythonheidong.com/blog/article/74/ 来源:python黑洞网 有什么 ...

  6. DAG模型(矩形嵌套)

    推荐在线例题:http://acm.nyist.net/JudgeOnline/problem.php?pid=16 题摘: 矩形嵌套 时间限制:3000 ms  |  内存限制:65535 KB 难 ...

  7. HTML基础知识 table中 th,td,tr

    https://www.2cto.com/kf/201711/701872.html table是一个布局神器,之前看过很多代码,都是用table布局的.但是,我在学习的过程中,发现table有很迷的 ...

  8. Android OkHttp与物理存储介质缓存:DiskLruCache(2)

     Android OkHttp与物理存储介质缓存:DiskLruCache(2) 本文在附录文章8,9的基础之上,把Android OkHttp与DiskLruCache相结合,综合此两项技术,实 ...

  9. Codeforces Round #321 (Div. 2)-B. Kefa and Company,区间最大值!

    ->链接在此<- B. Kefa and Company time limit per test 2 seconds memory limit per test 256 megabytes ...

  10. 测试各种低价VPS

    1) dream.jp 540多的日元一个VPS,是全日本最低的VPS,但是用了以后发现最大问题是受限很多,不好用,如果你打算用作建ss或者其它***功能,对不起,请找其它VPS了 在日本dream. ...