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. ArcGIS Engine控件运行许可(转)

    ArcGIS Engine控件运行许可   Runtime绑定: 在ArcGIS Engine10.0中,许可方式发生了一定的变化,ArcGis10有一个新的要求---runtime绑定.就是在任何A ...

  2. angularJS 2.0 开发的简单dome

    与其说这是一个demo,不如说是一个与高手的讨教过程.虽然demo出来了,可其中有些问题我还是不太明白,如果有angularjs2.0的大神,请进来指导一番,~~~~~不甚感激. 说明第一点:我采用的 ...

  3. First commit

    今天是2016年11月14日.天气晴. 第一篇博客,准备在这里记录下我学到的技术,希望能够坚持下来.^.^

  4. 关于tomcat内路径跳转的一些思考

    初学jsp+servlet时经常碰上的几个错误:404.路径正确但页面没有任何内容.样式和图片丢失. 这几个错误曾经让我在debug时头大,现在总结一下,其实它们都跟路径有关,正是因为没有处理好路径跳 ...

  5. [IOS 开发] TableView、多个TableViewCell、自定义Cell、Cell上画画(故事板+代码方式)

    第一步: //UserTableViewCell.h这里定义第一种Cell #import <UIKit/UIKit.h> @interface UserTableViewCell : U ...

  6. Swiper说明&&API手册 【中文手册Swiper】

     原文地址:http://www.cnblogs.com/scavengers/p/3760449.html 示例: <link rel="stylesheet" href= ...

  7. Linux主机共享目录给Windows主机的方法

    Linux主机共享目录可以通过samba来实现 首先,来看下百科上关于samba的介绍: Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Ser ...

  8. Android中ActionBar的使用

    简介 从Android3.0开始(targetSdkVersion或者minSdkVersion为11或者更高),ActionBar被包括在了所有主题为Theme.holo(或者子类)的主题当中. 使 ...

  9. Android中Activity的启动模式

    简介 Android中的活动启动方式分为4种:standard, singleTop, singleTask, singleInstance.可以在AndroidManifest.xml中通过给< ...

  10. vs2012 error c4996: This function or variable may be unsafe

    编译lua源码时,使用vs2012,遇到如下错误. 1>------ 已启动生成: 项目: 20130925, 配置: Debug Win32 ------ 1>  stdafx.cpp ...