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网络爬虫开发实战] 7.2-Splash的使用

    Splash是一个JavaScript渲染服务,是一个带有HTTP API的轻量级浏览器,同时它对接了Python中的Twisted和QT库.利用它,我们同样可以实现动态渲染页面的抓取. 1. 功能介 ...

  2. [Python3网络爬虫开发实战] 1.9.3-Scrapyd-Client的安装

    在将Scrapy代码部署到远程Scrapyd的时候,第一步就是要将代码打包为EGG文件,其次需要将EGG文件上传到远程主机.这个过程如果用程序来实现,也是完全可以的,但是我们并不需要做这些工作,因为S ...

  3. 利用WITH AS改写SQL

    报表程序中一段SQL语句. 优化前: 返回:3952 耗时:224s SQL 代码: select to_date(nvl(pro.value, '1900-01-01 00:00:00'), 'YY ...

  4. buf.keys()

    buf.keys() 返回:{Iterator} 创建并返回一个包含 Buffer 键名(索引)的迭代器. const buf = Buffer.from('buffer'); for (var ke ...

  5. 对SpringMVC框架的理解(转)

    SpringMVC概念:     他是一个轻量级的开源框架,应用于表现层,基于MVC的设计模式. SpringMVC的特点:     1.他是单例的可以设置成多例.     2.他的线程是安全的    ...

  6. node学习的一些网站

    Node.js 包教不包会 篇幅比较少 node express 入门教程 nodejs定时任务 一个nodejs博客 [NodeJS 学习笔记04]新闻发布系统 过年7天乐,学nodejs 也快乐 ...

  7. 全文搜索(A)-相关性

    文章:搜索相关性 文章:推荐系统中相似度算法介绍及效果测试 文章:常用的相似度计算方法原理及实现 文章:推荐系统用户相似度计算方法研究

  8. hdu 1814 2-sat 输出字典最小的和任意序列的 模板题

    /* 思路:http://blog.csdn.net/string_yi/article/details/12686873 hdu 1814 输出字典序最小的2-sat */ #include< ...

  9. Thinkphp5.0 的视图view的循环标签

    Thinkphp5.0 的视图view的循环标签 volist标签: <!-- 使用volist --> <!-- name是传递过来的要循环变量名 --> <!-- k ...

  10. hdu - 1104 Remainder (bfs + 数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=1104 注意这里定义的取模运算和计算机的%是不一样的,这里的取模只会得到非负数. 而%可以得到正数和负数. 所以需 ...