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 ...
随机推荐
- bzoj1455
学习了一下可合并堆的一种写法——左偏树感觉左偏树是一种类似启发式的方法学习左偏树后这题就水过去了 ..] of longint; v:..] of boolean; i,n,m,x,y,f:longi ...
- poj3167
这道题是一道kmp的扩展版的好题一串匹配一串很容易想到kmp,但是这里的匹配要求的是两个串的名次相同显然名次是会变的,为了方便,我们可以换一种表达对于两个等长的串的相同位置,名次相等就是在它之前比它小 ...
- 锐浪应用小插曲,asp.net下的使用
下午提前完成了今天的工作内容,整了下bs中的应用,嘿嘿,其中遇到不少问题,接下来说下大概会遇到哪些问题,1:grid++ 6.0插件下载安装之后ie浏览器无法打开,居然什么都没有显示,奇葩啊,系统版本 ...
- checkbox操作
小小示例:自己备份顺便粘出来共享. 引入头部文件:<script src="../js/jQuery1.7.2.js"></script> HTML代码: ...
- Eclipse中svn图标不显示
在菜单栏中:windows ->preferences->General->Appearance->Lable Decorations 勾选其中的 SVN 项,最后应用确认之后 ...
- Struts1、Struts2和SpringMVC剖析【转载】
前段框架用了不少,今天就来做个总结.网上关于Struts1.Struts2.SpringMVC的文章有很多,这里的内容就是基于它们,来做个比较. 这三个框架是按照上面的顺序,依次出现的,它们都是对MV ...
- python 3 处理HTTP 请求的包
http http: https://docs.python.org/3/library/http.html http是一个包,里面含有多个模块:http.client,http.server,htt ...
- 最常用的 Eclipse 快捷键总结
本文是一些最实用.最齐全.最省时间的 Eclipse 快捷键总结,这些快捷键可以让帮助你完成工作中的任何一个操作.欢迎参考. 1. ctrl+shift+r:打开资源 这可能是所有快捷键组合中最省时间 ...
- Google表格
本博文的主要内容有 .Google表格的介绍 https://www.google.com/intl/zh-CN/sheets/about/ https://accounts.google.com/ ...
- android 随手记 倒计时
class CountDownUtils extends CountDownTimer { public CountDownUtils(long millisInFuture, long countD ...