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 ...
随机推荐
- .Net Core 做请求监控NLog
使用 NLog 给 Asp.Net Core 做请求监控 https://www.cnblogs.com/cheesebar/p/9078207.html 为了减少由于单个请求挂掉而拖垮整站的情况发生 ...
- 050 Pow(x, n)
实现 pow(x, n).示例 1:输入: 2.00000, 10输出: 1024.00000示例 2:输入: 2.10000, 3输出: 9.26100详见:https://leetcode.com ...
- Hart协议
官方https://fieldcommgroup.org/technologies/hart/documents-and-downloads-hart 参考网页http://www.eeworld.c ...
- 数据库(数据库、表及表数据、SQL语句)
数据库MYSQL 今日内容介绍 u MySQL数据库 u SQL语句 第1章 数据库 1.1 数据库概述 l 什么是数据库 数据库就是存储数据的仓库,其本质是一个文件系统,数据按照特定的格式将数据存储 ...
- 通过Maven构建打包Spring boot,并将config配置文件提取到jar文件外
如果通过不同的IDE打包,着实会觉得依赖性太大,并且容易出现错误,操作也比较复杂 同时,spring-boot-maven-plugin的使用感觉,相关配置太少,并且无法满足方便部署和运行的需求. 这 ...
- 解决Chrome浏览器自动记录用户名和密码的黄色背景问题和该解决方法与tab切换至下一个input冲突的问题。
哈哈哈,是不是标题很长呀,不逗你们了.其实这么长的标题主要就说了两件事: 第一件:解决Chrome浏览器自动记录用户名和密码的黄色背景问题. 第二件:输入完用户名后按下tab键切换至下一个输入密码in ...
- 洛谷P2430 严酷的训练
第一眼看这道题...啊哈,啥??? 仔细看一看,发现:诶, 这不是01背包吗? 两人水平值的比值*老王做题用时 可以算出WKY做每道题的用时. 那么每道题的p就可以转换成费用c[i], 价值q就是w[ ...
- ES6学习(2)
Set和Map数据结构 ES6 提供了新的数据结构 Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set 本身是一个构造函数,用来生成 Set 数据结构. const s = new ...
- ubuntu 12.04 source.list 源更新
官方源: #deb cdrom:[Ubuntu 12.04 LTS _Precise Pangolin_ - Release i386 (20120423)]/ precise main restri ...
- Bibtex使用介绍
BibTeX 是一种格式和一个程序, 用于协调LaTeX的参考文献处理. BibTeX 使用数据库的的方式来管理参考文献. BibTeX 文件的后缀名为 .bib . 先来看一个例子 @article ...