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?

想要做出来很简单,但是关键在于这里的最后一句话,要在O(1)时间内,而且不能使用循环或者递归。

这里就要说说有关数字根的概念了:

定义

数字根(Digital Root)就是把一个数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止。而这个一位数便是原来数字的数字根。适用范围为正整数和零。例如:

1的数字根为1

10的数字根为1(1+0=1)

21的数字根为3(2+1=3)

48的数字根为3(4+8=12,1+2=3)

198的数字根为9(1+9+8=18,1+8=9)

性质说明

1.任何数加9的数字根还是它本身。

       小学学加法的时候我们都明白,一个数字加9,就是把十位加1,各位减1。因此十位加个位的和是不变的;如果有进位,即十位上是9,那么进位之后十位会变成0,百位会加1,道理和一个一位数加9是一样的。

2.9乘任何数字的数字根都是9。

      同样是小学时学乘法时,我们在计算一位数乘九的时候,把十只手指头排开,乘几便弯下第几只手指头,前后的手指个数便是那个结果。它的数字根永远是10-1=9。多位数的化,拆分每一位数字即可。

3.数字根的三则运算

  1.两数之和的数字根等于这两个数的数字根的和数字根

       对于两个一位数来说,很容易理解。因为一位数的数字根就是它本身。
       对于多位数来说,由性质1,把每个数字mod 9,就又变成了两个一位数。

  2.两数之积的数字根等于这两个数的数字根的积的和数字根

       可以把每个数字拆成许多9相加的形式,最后各剩余一个 (a mod 9), 由
            (a1+a2+...)*(b1+b2+...)=a1*(b1+b2+...)+a2*(b1+b2+...)+...+an*bm
       从a1到a[n-1]都是9,由性质2,原来两式的数字根就是(an*bm)的数字根。而由性质1,可知an,bm又是两数本身的数字根。

  3.一个数字的n次幂的数字根等于这个数字的数字根的n次幂的和数字根

  这一部分是转载 数字根介绍 的;

  所以可以得知计算数字根的公式就是:root = (num - 1) % 9 + 1;  PS:这里的 -1主要是为了避免num正好是9的倍数,那样数根应该正好是9, 而不应该是0才对。

  代码不言而喻,只不过道理应该想清楚才对:

  考虑一下 result = (num - 1) % 9 + 1; 那么就有

      result - 1 = (num - 1) % 9; 那么 digitRoot(result - 1) == digitRoot(num - 1);

  那么根据上面所说的,就有:result的数字根与num的数字根就是一样的了。所以这个等式成立。

 class Solution {
public:
int addDigits(int num) {
return (num - )% + ;
}
};

java版本代码如下所示:

public class Solution{
public int addDigits(int num){
return (num - 1)%9 + 1;
}
}

LeetCode OJ:Add Digits(数字相加)的更多相关文章

  1. LeetCode:Add Digits - 非负整数各位相加

    1.题目名称 Add Digits (非负整数各位相加) 2.题目地址 https://leetcode.com/problems/add-digits/ 3.题目内容 英文:Given a non- ...

  2. [LeetCode] 415. Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  3. 【LeetCode】Add Digits

    Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...

  4. LN : leetcode 258 Add Digits

    lc 258 Add Digits lc 258 Add Digits Given a non-negative integer num, repeatedly add all its digits ...

  5. LeetCode 258 Add Digits(数字相加,数字根)

    翻译 给定一个非负整型数字,反复相加其全部的数字直到最后的结果仅仅有一位数. 比如: 给定sum = 38,这个过程就像是:3 + 8 = 11.1 + 1 = 2.由于2仅仅有一位数.所以返回它. ...

  6. [LeetCode] 258. Add Digits 加数字

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  7. Java [Leetcode 258]Add Digits

    题目描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...

  8. LeetCode 258. Add Digits

    Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...

  9. LeetCode 258 Add Digits 解题报告

    题目要求 Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...

  10. (easy)LeetCode 258.Add Digits

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

随机推荐

  1. linux一路填坑...

    1.安装ubuntu 从ubuntu9.0开始,一路更新,越来越垃圾,更可恶的是工作上经常指定特定的版本,于是乎,我电脑里装了n个版本的ubuntu. Win7 + Ubuntu 15.10 1)装完 ...

  2. django 中的路由系统(url)

    路由系统 根据Django约定,一个WSGI应用里最核心的部件有两个:路由表和视图.Django框架 的核心功能就是路由:根据HTTP请求中的URL,查找路由表,将HTTP请求分发到 不同的视图去处理 ...

  3. python16_day13【css、js】

    一.部局 1.pg-header(title) .pg-header{ height: 48px; background-color: cadetblue; line-height: 48px; mi ...

  4. python16_day03【集合、编码、函数、递归、内置函数】

    一.集合 集合是一个无序的,不重复的数据组合,它的主要作用如下: 去重,把一个列表变成集合,就自动去重了 关系测试,测试两组数据之前的交集.差集.并集等关系 #创建: s = {3,5,9,10} # ...

  5. begoo——路由设置

    路由本质是URL与要为该URL调用的视图函数之间的映射表,其实就是你定义的使用那个URL调用那段代码的关系对应表. 首先看一下最简单的路由: package routers import ( &quo ...

  6. python全栈开发从入门到放弃之元组的内置应用

    1.元组的字符类型tuple t=(1,[1,3],'sss',(1,2)) print(type(t)) <class 'tuple'> 2.按索引号取值 t=(1,[1,3],'sss ...

  7. xshell下载安装

    打开网址http://www.netsarang.com/download/software.html 找到最新版的xshell,点击下载 在跳转的页面填写个人信息,许可证类型选择家庭和学校使用,除了 ...

  8. SpringBoot RedisMQ消息队列与发布订阅

    SpringBoot简单整合RedisMQ消息队列和发布订阅 注:RedisMq消息队列使用redis数组实现,leftpush存一,rightpop取一. 1.application.propert ...

  9. sublime批量替换文本重复单词

    事先需要把单词打到文本的每一行 排序 按F9或者选择菜单:Edit > Sort Lines,对每行文本进行排序 查找重复行 排序好后,按Ctrl+F,调出查找面板 查找字符串: ^(.+)$[ ...

  10. 编译安卓7.0源码出现make: *** [ninja_wrapper] Error 1【转】

    本文转载自:https://blog.csdn.net/u010684585/article/details/76263317 32 warnings generated.ninja: build s ...