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网络爬虫开发实战] 6.1-什么是Ajax

    Ajax,全称为Asynchronous JavaScript and XML,即异步的JavaScript和XML.它不是一门编程语言,而是利用JavaScript在保证页面不被刷新.页面链接不改变 ...

  2. select into outfile 与 load data infile

    select into outfile用法 MySQL中,可以使用SELECT...INTO OUTFILE语句将表的内容导出为一个文本文件. SELECT [列名] FROM table [WHER ...

  3. Cropping multiple images the same way

    The tools we’ll be using are =GIMP= and =mogrify= (from the ImageMagick suite), so make sure that yo ...

  4. Leetcode 149.直线上最多的点数

    直线上最多的点数 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | |        o ...

  5. 第五章、 Linux 常用網路指令

    http://linux.vbird.org/linux_server/0140networkcommand.php     第五章. Linux 常用網路指令 切換解析度為 800x600 最近更新 ...

  6. bzoj1007 [HNOI2008]水平可见直线 - 几何 - hzwer.com

    Description Input 第一行为N(0 < N < 50000),接下来的N行输入Ai,Bi Output 从小到大输出可见直线的编号,两两中间用空格隔开,最后一个数字后面也必 ...

  7. Django:(4)Django和Ajax

    向服务器发送请求的途径: 1. 浏览器地址栏,默认get请求 2. form表单: get请求: post请求 3. a标签,默认get请求 4. Ajax:get请求:post请求 Ajax的特点( ...

  8. 2015山东信息学夏令营 Day5T3 路径

    问题描述: 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. 输入: 第一行包含一个正整数n,表示点数. 接下来 ...

  9. 【IntelliJ】IntelliJ IDEA的安装破解及使用

    结合两位大牛CV的,写的很全面,仅供自己使用 转载地:http://www.jianshu.com/p/ad3830095fb3 https://www.cnblogs.com/kangjianwei ...

  10. Session&Cookie 的介绍和使用

    Session介绍与使用 1.Session基本介绍 Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及配置信息.这样,当用户在应用程序 ...