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. node函数assert()详解

    assert.ok() 的别名. const assert = require('assert'); assert(true); // OK assert(1); // OK assert(false ...

  2. Mac 共享无线网络

    dock栏启动『设置』进入下一步.   进入设置后,点击『共享』进入下一步.   进入共享后 1.选择左边框中『互联网共享』,就会看到截图右边的样子 2.『共享以下来源链接』一项选择『以太网』(默认应 ...

  3. Python中表示中文的pattern

    Python中表示中文的pattern:[\u4e00-\u9fff] 汉字unicode码表: http://jlqzs.blog.163.com/blog/static/2125298320070 ...

  4. jquery动态绑定元素

    按照正常给静态元素绑定事件的写法换成给动态元素绑定事件会不管用,要用下面的方式: 简单说就是给要绑定元素的父元素绑定事件,参数中指明要绑定该父元素下面的哪个元素就行,这样就不管你这个元素是不是新增的, ...

  5. MySQL最优配置文件模板·2016-11-28

    小伙伴们大爱的MySQL最优配置文件模板更新啦.对之前的MySQL最优配置文件·20160901做了一些修正,更为名至实归.可以通过此链接进行下载.当然,更欢迎同学们提出意见和建议,共同打造一个最优M ...

  6. K-th Number POJ - 2104 划分树

    K-th Number You are working for Macrohard company in data structures department. After failing your ...

  7. msp430入门编程06

    msp430中C语言的程序结构06 msp430中C语言的函数及实现07 msp430中C语言操作端口I/O10 msp430中C语言的模块化头文件及实现11 msp430中C语言的模块化头文件及库文 ...

  8. Linux下汇编语言学习笔记44 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  9. android中后一个activity传值给前一个activity的实现

    前一个activity跳转到后一个activity设置code: Intent intent=new Intent(MainActivity.this,ActivityTwo.class); star ...

  10. hdu - 1627 Krypton Factor (dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1627 给定 n 和 L 找出第n个范围在0-L之内的字符串,字符串要求没有相邻的子串是相同的. 按照格式输出. ...