题目

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?

分析

有一个非负整数num,重复这样的操作:对该数字的各位数字求和,对这个和的各位数字再求和……直到最后得到一个仅1位的数字(即小于10的数字)。

例如:num=38,3+8=11,1+1=2。因为2小于10,因此返回2。

第一个解法,利用题目要求的规律,循环计算得出。

题目后续问能否用O(1)解法,这就需要深入研究一下计算规律了。

举例说明:

假设输入的数字是一个5位数字num,则num的各位分别为a、b、c、d、e。

有如下关系:num = a * 10000 + b * 1000 + c * 100 + d * 10 + e

即:num = (a + b + c + d + e) + (a * 9999 + b * 999 + c * 99 + d * 9)

因为 a * 9999 + b * 999 + c * 99 + d * 9 一定可以被9整除,因此num模除9的结果与 a + b + c + d + e 模除9的结果是一样的。

对数字 a + b + c + d + e 反复执行同类操作,最后的结果就是一个 1-9 的数字加上一串数字,最左边的数字是 1-9 之间的,右侧的数字永远都是可以被9整除的。

这道题最后的目标,就是不断将各位相加,相加到最后,当结果小于10时返回。因为最后结果在1-9之间,得到9之后将不会再对各位进行相加,因此不会出现结果为0的情况。

因为 (x + y) % z = (x % z + y % z) % z,又因为 x % z % z = x % z,

因此结果为 (num - 1) % 9 + 1,只模除9一次,并将模除后的结果加一返回。

来自:参考链接

AC源码

class Solution {
public:
//常规方法计算
int addDigits1(int num) {
if (num / 10 == 0)
return num; while (num / 10 != 0)
{
//求出num的各个位数字之和
int tmp = 0;
while (num)
{
tmp += num % 10;
num /= 10;
}//while
num = tmp;
}//while
return num;
}
//结果为:(num - 1) % 9 + 1
int addDigits(int num) {
return (num - 1) % 9 + 1;
} };

GitHub测试程序源码

LeetCode(258) Add Digits的更多相关文章

  1. leetcode之旅(6)-Add Digits

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

  2. LeetCode(2)Add Two Numbers

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

  3. LeetCode(67) Add Binary

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

  4. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  5. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  6. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  7. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  8. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  9. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

随机推荐

  1. 线程池(2)Executors.newFixedThreadPool

    例子: ExecutorService es = Executors.newFixedThreadPool(5); try { for (int i = 0; i < 20; i++) { Ru ...

  2. github 新建一个分支

    我能说今天在github上新建分支的时候懵逼了半天吗..为了下次不再懵逼,还是在这里记录一下吧.. 进入你的项目---code---Branch----点击那个倒三角-----你会发现一个输入框(这是 ...

  3. 开园了,将以此记录个人web前端之路

    记录.分享与学习 2015年5月中旬开始学习web前端到2015年6月底找到第一份相关工作,在学习与工作过程中通过网络获益良多,在此写下个人学习与工作过程中的总结与思考,记录个人成长,同时也希望能够帮 ...

  4. springboot项目实现批量新增功能

    这个困扰我一整天东西,终于解决了. 首先是mybatis中的批量新增sql语句. 注意:这里我给的是我需要新增的字段,你们改成你们需要的字段. <insert id="insertBa ...

  5. Java虚拟机内存分配与回收策略

    内存分配与回收策略 Minor GC 和 Full GC Minor GC:发生在新生代上,因为新生代对象存活时间很短,因此 Minor GC 会频繁执行, 执行的速度一般也会比较快. Full GC ...

  6. JAVA---spring-boot入门(图文教程)

    Spring Boot可以轻松创建独立的,生产级的基于Spring的应用程序,他的特征:    1.创建独立的Spring应用程序    2.直接嵌入Tomcat,Jetty或Undertow(无需部 ...

  7. https的网站使用百度地图的问题

    https的网站使用百度地图,如果你引用的地址没写对的话,加载不出来百度地图,被认为是不安全的JS内容. 引用的地址:http://api.map.baidu.com/api?v=2.0&ak ...

  8. JavaScript笔记4-数组

    一.概述: 1.数组是无类型的:同一数组的各元素可以是任意类型,也可以是数组或对象; 2.索引从0开始,最大到2^32-2=4294967294;最多容纳4294967295个元素; 3.数组是动态的 ...

  9. Ubuntu 11.04 安装 cuda5.0

    由于实验需要,于2016年10月15日再Ubuntu11.04安装cuda5.0,但是从网上查找Ubuntu11.04 只有对应的支持的cuda4 版本,cuda 5.0前面版本不支持IDE nisg ...

  10. UI设计中蕴涵着系统重要的数据结构与功能设计

    UI设计中蕴涵着系统重要的数据结构与功能设计 UI设计中的用户需求,事件(用例)驱动