LeetCode(258) 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 = 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;
}
};
LeetCode(258) Add Digits的更多相关文章
- leetcode之旅(6)-Add Digits
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
- LeetCode(2)Add Two Numbers
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- LeetCode(67) Add Binary
题目 Given two binary strings, return their sum (also a binary string). For example, a = "11" ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- 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 ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- 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 ...
随机推荐
- Linux上使用VIM进行.Net Core
如何在Linux上使用VIM进行.Net Core开发 对于在Linux上开发.Net Core的程序员来说, 似乎都缺少一个好的IDE.Windows上有Visual Studio, Mac上有Vi ...
- 5. 把一幅彩色图像的H、S、I分量单独显示。
#include <cv.h> #include <highgui.h> # include <math.h> #define M_PI 3.1415 void R ...
- setTimeout() 实现程序每隔一段时间自动执行
定义和用法 setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式. 语法 setTimeout(code,millisec) 参数 描述 code 必需.要调用的函数后要执行的 Ja ...
- RESTful API设计相关
一 RESTful架构 在当今时代,越来越多人意识到了网站即软件. 这种"互联网软件"采用客户端/服务器模式,建立在分布式体系上,通过互联网通信,具有高延时(high laten ...
- 死磕 java原子类之终结篇(面试题)
概览 原子操作是指不会被线程调度机制打断的操作,这种操作一旦开始,就一直运行到结束,中间不会有任何线程上下文切换. 原子操作可以是一个步骤,也可以是多个操作步骤,但是其顺序不可以被打乱,也不可以被切割 ...
- ABAP接口用法
1.定义接口INTERFACE intf [PUBLIC]. [components] ENDINTERFACE. 2.注意点: 2.1.接口中所定义的所有东西默认都是公共的,所以不用也不能写PU ...
- jmeter参考网址
http://blog.csdn.net/dongdong9223/article/details/49248979 http://blog.csdn.net/hjh00/article/detail ...
- Python之邮件发送
Python的smtplib提供了一种很方便的途径用来发送电子邮件,它有SMTP协议进行简单的封装,可以使用SMTP对象的sendmail方法发送邮件,通过help()查看SMTP所提供的方法如下: ...
- 洛谷 P1903 【模板】分块/带修改莫队(数颜色)
题目描述 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔. 2 ...
- Ruby中访问控制符public,private,protected区别总结
重点关注private与protected public 默认即为public,全局都可以访问,这个不解释 private C++, “private” 意为 “private to this cla ...