258. Add Digits(C++)
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,重复地将其每位数字相加,直到结果只有一位数为止。
例如:
给定 num = 38,过程像这样:3 + 8 = 11, 1 + 1 = 2。因为2只有一位,返回之。
进一步思考:
你可以不用循环,在O(1)运行时间内完成题目吗?
解题方法:
1.模拟上述方法(递归)。
2.观察法。
使用方法I的代码循环输出0 - 19的运行结果:
in out in out
0 0 10 1
1 1 11 2
2 2 12 3
3 3 13 4
4 4 14 5
5 5 15 6
6 6 16 7
7 7 17 8
8 8 18 9
9 9 19 1
可以发现输出与输入的关系为:
out = (in - 1) % 9 + 1
注意事项:
C++代码:
1.递归:
class Solution
{
public:
int addDigits(int num)
{
int tmp=num/+num%;
if(tmp<)
{
return tmp;
}
else
{
num=tmp;
tmp=addDigits(num);
}
return tmp;
}
};
2.观察法:
class Solution
{
public:
int addDigits(int num)
{
if (num == )
return ;
return (num - ) % + ;
}
};
258. Add Digits(C++)的更多相关文章
- LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree
258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...
- LN : leetcode 258 Add Digits
lc 258 Add Digits lc 258 Add Digits Given a non-negative integer num, repeatedly add all its digits ...
- 【LeetCode】258. Add Digits (2 solutions)
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- LeetCode 258. Add Digits
Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...
- (easy)LeetCode 258.Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- 258. Add Digits
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
- Java [Leetcode 258]Add Digits
题目描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...
- 【LeetCode】258. Add Digits
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
- 【一天一道LeetCode】#258. Add Digits
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- PuTTY 'modmul()' 函数缓冲区下溢漏洞(CVE-2013-4206)
漏洞版本: PuTTY 0.52 - 0.63 漏洞描述: BUGTRAQ ID: 61645 CVE(CAN) ID: CVE-2013-4206 PuTTY是Windows和Unix平台上的PuT ...
- @repository注解
Spring的注解形式:@Repository.@Service.@Controller,它们分别对应存储层Bean,业务层Bean,和展示层Bean. @Repository.@Service.@C ...
- 使用LoadRunner对Web Services进行调用--Import Soap
利用LoadRunner对Web Services进行测试时,通常有三种可供采用的方法: 在LoadRunner的Web Services虚拟用户协议中,[Add Service Call] 在Loa ...
- 细谈Java
重载:相同函数名,不同参数. 重写(覆写):父类和子类之间的,子类重写了父类的方法. java的多态:重载+覆写 1. Main方法: 是public的,也是static,也是void的,参 ...
- python 开发简单的聊天工具
python 太强大了,以至于它什么都可以做,哈哈,开个玩笑.但是今天要讲的真的是一个非常神奇的应用. 使用python写一个聊天工具 其实大家平时用的QQ类似的聊天工具,也是使用socket进行聊天 ...
- OBJ解析
OBJ文件是Alias|Wavefront公司为它的一套基于工作站的3D建模和动画软件"Advanced Visualizer"开发的一种标准3D模型文件格式,很适合用于3D软件模 ...
- Ubuntu开机出现:Fontconfig warning:"/etc/fonts/conf.d/65-droid-sans-fonts.conf"的解决办法
Ubuntu升级后可能会出现以下问题: Fontconfig warning: "/etc/fonts/conf.d/65-droid-sans-fonts.conf", line ...
- HDOJ 4696 Answers 乱搞
乱搞: rt.有1就能输出全部的数,否则仅仅能输出偶数 Answers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/1 ...
- qt 学习之路 :QML 语法
前面我们已经见识过 QML 文档.一个 QML 文档分为 import 和对象声明两部分.如果你要使用 Qt Quick,就需要 import QtQuick 2.QML 是一种声明语言,用于描述程序 ...
- [转] JavaScript中的属性:如何遍历属性
在JavaScript中,遍历一个对象的属性往往没有在其他语言中遍历一个哈希(有些语言称为字典)的键那么简单.这主要有两个方面的原因:一个是,JavaScript中的对象通常都处在某个原型链中,它会从 ...