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. (转抄:人人都是产品经理——iamsujie)如何提高产品规划PPT的能力

    做产品几年之后,不可避免的要碰到写规划这件事儿,虽说不少人,不乏大牛,对规划持“无用论”的观点, 但大多数人其实还是更相信这个段子: 在一个公司里,看一个人的地位,主要看他平时写的文档类型——写wor ...

  2. 强大的命令行工具wmic

    1.wmic=Microsoft Windows Management Instrumentation 2. C:\WINDOWS\system32\wbem 下的东西,特别是.xsl格式化文件,实现 ...

  3. iframe自动高度

    <script> //设置iframe自动高度 function setIframe(id){ var fn = function(){ try{ var iframe = typeof ...

  4. Wind7外接显示器选择拓展模式后,鼠标只能往右移动才能切换到外接显示器上,不能修改切换方向

    win7外接显示器选择拓展模式后,为什么鼠标只能往右移动才能切换到外接显示器上,不能修改切换方向 打开控制面板-->显示 其他不变的情况下,鼠标拖动上面的两个显示器图标,拉出你希望的方向即可.

  5. [转]用CSS给SVG <use>的内容添加样式

    来源:http://www.w3cplus.com/svg/styling-svg-use-content-css.html?utm_source=tuicool&utm_medium=ref ...

  6. iOS:iOS中的多控制器管理

    iOS中的控制器有三种创建方式: 1.通过storyboard创建 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@" ...

  7. [IOS 开发代码]UIImage+Blur 网络图片模糊用法

    UIImage-Helpers 网络图片模糊用法   float quality = .00001f;    float blurred = .5f; NSURL *url = [NSURL URLW ...

  8. Mysql Innodb 引擎优化-内存、日志、IO、其他相关参数

    介绍: InnoDB给MySQL提供了具有提交,回滚和崩溃恢复能力的事务安全(ACID兼容)存储引擎.InnoDB锁定在行级并且也在SELECT语句提供一个Oracle风格一致的非锁定读.这些特色增加 ...

  9. 关于RSA加密算法的长度限制问题

    RSA是常用的非对称加密算法.近来有学生在项目中使用System.Security类库中的RSA加密算法时,出现了“不正确的长度”,这实际上是因为待加密的数据超长所致..net Framework中提 ...

  10. AppFog免费云空间申请及安装wordpress(图文教程)

    AppFog是一家提供运算平台的服务,用户可以在上面搭建自己的Web App.原本它的名字为PHPFog,但在采用了Cloud Foundry的代码作为核心,支持多个编程语言后,选择了更名.AppFo ...