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. [Python3网络爬虫开发实战] 3.1.2-处理异常

    前一节我们了解了请求的发送过程,但是在网络不好的情况下,如果出现了异常,该怎么办呢?这时如果不处理这些异常,程序很可能因报错而终止运行,所以异常处理还是十分有必要的. urllib的error模块定义 ...

  2. oracle dmp文件的导入导出

    一.命令行方式 exp 用户名/密码@库名 file=文件位置.dmp owner=用户名 imp 用户名/密码@库名 file=文件位置.dmp 注意 : 导入过程若有的表已经存在可能会报错,可以全 ...

  3. intellij idea 忽略文件不提交

    文件已经纳入版本管理 如果文件已经纳入版本了,应该采用此方法 此方法主要应对文件已经纳入版本管理,但不想再提交,比如,不小心提交的eclipse.intellij的文件,以后不想再提交了,这种就通过v ...

  4. Leetcode 213.大家劫舍II

    打家劫舍II 你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的.同时,相邻的房屋装有相互连通的防盗系统,如果两 ...

  5. 九度oj 题目1181:遍历链表

    题目1181:遍历链表 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3483 解决:1465 题目描述: 建立一个升序链表并遍历输出. 输入: 输入的每个案例中第一行包括1个整数:n(1 ...

  6. 全文索引(A-1)-用户数据收集(用户研究)

    推荐系统根据用户的信息和历史行为记录,构造出用户的个性化模型,再依据特定的推荐算法,向用户推荐其可能感兴趣的项目. 如何获取用户的偏好? 建议用户对一些指定项目进行评分,如对:小说.传记.技术书.图画 ...

  7. python-gzip解压缩(实验吧SOS)

    本题看着很简单,就是在弄出来的老是乱码,看了pcat的wp还是不行,下面的评论说可能是python版本问题,改版本太麻烦,试了一下先gzip解压,得到的文件在打开就不是乱码了,代码如下: # -*- ...

  8. PatentTips - Register file supporting transactional processing

    BACKGROUND OF THE INVENTION With the rise of multi-core, multi-threaded data processing systems, a k ...

  9. POJ 3723 Conscription【最小生成树】

    题意: 征用一些男生和女生,每个应都要给10000元,但是如果某个男生和女生之间有关系,则给的钱数为10000减去相应的亲密度,征集一个士兵时一次关系只能使用一次. 分析: kruskal求最小生成树 ...

  10. Ubuntu 16.04安装unrar解压RAR文件

    除了7zip:http://www.cnblogs.com/EasonJim/p/7124306.html之外,还可以安装unrar进行解压RAR文件. 安装 sudo apt-get install ...