Add Digits

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 = 111 + 1 = 2. Since 2 has only one digit, return it.

Follow up:
Could you do it without any loop/recursion in O(1) runtime?


Solution:

考虑 O(1) 的算法,从最朴素的数字开始:

0 - 9 无疑都对应返回 0 - 9 就行了。

接下来,

10 返回 1

11 返回 2

12 返回 3

13 返回 4

……

18 返回 9

19 返回 1

……

规律呼之欲出,因为最终所有的数字都将被 映射到 0 - 9 这 10 个数字上,所以才开始考虑这样的映射是否存在一些本质上的规律,那么由上述可以发现规律就是随着数字本身的大小递增,最后映射到的数其实也是递增的,但这样的递增其实是以 9 为模,而对于此题我们最后只关心取模后的余数而不关心数字本身有几个“模9”了。

需要注意的一点是:

0 在这里是特殊的,只有 0 数字本身映射到 0 ,再也没有其他数字映射到 0,需要单独处理。

所以实际上 我们是将 除0外的数字 映射 到 1-9 这个数字上,想想便知结果应该是 (num-1) % 9 + 1。

代码如下:

 class Solution:
# @param {integer} num
# @return {integer}
def addDigits(self, num):
if num == 0:
return 0
else:
return (num-1) % 9 + 1

【LeetCode】Add Digits的更多相关文章

  1. 【02_258】Add Digits

    Add Digits Total Accepted: 49702 Total Submissions: 104483 Difficulty: Easy Given a non-negative int ...

  2. 【leetcode】Add Two Numbers

    题目描述: You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  3. 【题解】【链表】【Leetcode】Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  4. 【leetcode】Add Two Numbers(middle) ☆

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  5. 【leetcode】 Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  6. 【LeetCode】Add Two Numbers(两数相加)

    这道题是LeetCode里的第2道题. 题目要求: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将 ...

  7. 【LeetCode】Reverse digits of an integer

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you ...

  8. 【leetcode】Add Binary

    题目简述: Given two binary strings, return their sum (also a binary string). For example, a = "11&q ...

  9. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

随机推荐

  1. 关于android的@TargetApi和@SuppressLint("NewApi")

    看别人的代码好多地方用到了@TargetApi.以前一直不知道这个是什么意思.后面偶然看了下sdk.才有所明白. 其实这个东西就是在你使用了android Lint检查工具的时候,为了防止代码出现提示 ...

  2. Dns 类

    Dns 类型公开以下成员. 方法       名称 说明 BeginGetHostAddresses 异步返回指定主机的 Internet 协议 (IP) 地址. BeginGetHostByName ...

  3. meta标签和作用

    <meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content=" ...

  4. maven私服搭建(centOS6.5)

    maven的好处和私服的应用本文不赘述,私服搭建如下: MAVEN 私服搭建(centOS 6.5 环境) 1.  准备环境,搭建centOS6.5系统环境,略 2.  准备对应的软件包如下: A. ...

  5. mysql 5.7配置文件参数详解

    read_buffer_size 默认大小:128KB 最大:2GB 最小:8KB 增量:必须为4KB的整数倍,如果配置的不是整数倍,会向下取整 用途: 1.MyISAM表顺序扫描提供的缓存 2.所有 ...

  6. 如何用fir.im 命令行工具 打包上传

    1.注册fir.拿到token 2.安装 fir-cli 使用 Ruby 构建, 无需编译, 只要安装相应 gem 即可. $ ruby -v # > 1.9.3 $ gem install f ...

  7. MYSQL获取自增ID的四种方法

    MYSQL获取自增ID的四种方法 1. select max(id) from tablename 2.SELECT LAST_INSERT_ID() 函数 LAST_INSERT_ID 是与tabl ...

  8. Android中的多线程编程

    问题 Android的UI也是线程不安全的,如果要更新应用程序里的UI元素,必须在主线程中进行,否则就会抛异常.比如用一个Button的onClick函数去更新界面上的元素,就会得到一个CalledF ...

  9. 支付宝APP支付后台参数生成Java版(一)

    一.支付参数组装: String[] parameters={ "service=\"mobile.securitypay.pay\"",//固定值 " ...

  10. Whole life

    Whole life the wonder such you bring the beautyI can see but i keep deep inside on itOh life i feel ...