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 ...
随机推荐
- Light OJ 1033 - Generating Palindromes(区间DP)
题目大意: 给你一个字符串,问最少增加几个字符使得这个字符串变为回文串. ============================================================= ...
- myeclipse如何修改Web项目名称
1. 使用myeclipse进行Web项目开发时,有时候项目名称重命名了,但是WEB项目在tomcat里边的名称还是原来的名称,是需要重新命名的.下边详细介绍一下如果和修改web项目名称,myecli ...
- 关于ATL的rgs注册文件
转自:http://blog.csdn.net/idiszerg/article/details/3875934 使用ATL向导的话,会在resource中产生一个rgs的注册脚本文件放在" ...
- 以O2O为核心的ERP思考
O2O已经火了一阵子了,很多人都在说O2O,各行各业都想和O2O有所结合,都认为这里面将会有巨大的商机. 在互联网发展到移动互联网的时代,我们的生活的很多方面已经被改变了,很多事情都已经可以在移动端完 ...
- Bridges painting - SGU 121(构造)
题目大意:有个一无向图,给所有的边染色,如果一个点连接的边超过两个,那么最少要染一个白色和一个黑色,能否给整个图染色?不能输出“No solution”. 分析:引用连接 http://edward- ...
- Eclipse下Tomcat常用设置
Eclipse下Tomcat常用设置 1,Eclipse建立Tomcat服务 1.1 新建Server 首先这里是指,jee版的Eclipse.Eclipse是没有像MyEclipse那样集成Tomc ...
- bzoj3907: 网格
http://www.cnblogs.com/Tunix/p/4354348.html #include<cstdio> #include<cstring> #include& ...
- Hibernate学习之对象持久化
1. 对象持久化 对象的持久化就是把内存中对象形式的业务数据,转换成数据库中的关系数据形式的业务数据.广义理解,对象的持久化还包括内存与关系数据库之交换业务数据的各种操作. 2. 对象持久化模式 1 ...
- 关于谷歌、火狐 右键没有发送到onenote选项
关于chrome .FF 右键没有发送到onenote选项 问题: 使用Microsoft office中的onenote作为自己平时学习和工作的 ...
- 经常使用虚拟现实仿真软件总汇(zz)
经常使用虚拟现实仿真软件总汇(zz)http://hi.baidu.com/busycai/blog/item/fe57e41e5f25fa1c403417b2.html 2007年09月07日 星 ...