LeetCode258——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 = 111 + 1 = 2
. Since 2
has
only one digit, return it.
实现:
class Solution {
public:
int addDigits(int num) {
int result = num;
while (true) {
if (result < 10) {
return result;
}
num = result;
result = 0;
while (num) {
result = result + num % 10;
num = num / 10;
}
}
}
};
LeetCode258——Add Digits的更多相关文章
- LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number
数学题 172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. N ...
- LeetCode258:Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- [LeetCode258] Add Digits 非负整数各位相加
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
- LeetCode 258. 各位相加(Add Digits)
258. 各位相加 258. Add Digits 题目描述 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. LeetCode258. Add Digits 示例: 输入: 3 ...
- 【LeetCode】Add Digits
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference
最近做的题记录下. 258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the ...
- 【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] Add Digits (a New question added)
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- 258. Add Digits(C++)
258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has ...
随机推荐
- [Swift通天遁地]三、手势与图表-(11)制作雷达图表更加形象表示各个维度的情况
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- MySQL-ProxySQL中间件(一)| ProxySQL基本概念
目录 MySQL-ProxySQL中间件(一)| ProxySQL基本概念: https://www.cnblogs.com/SQLServer2012/p/10972593.html ...
- ACM_栈的压入、弹出序列
栈的压入.弹出序列 Time Limit: 2000/1000ms (Java/Others) Problem Description: 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列 ...
- CSS——轮播图中的箭头
注意事项: 1.定位中left权重比right高,top权重比bottom高 2.两个span标签嵌套在一个盒子中,将来显示隐藏只需要控制父盒子就行了 <!DOCTYPE html> &l ...
- git生成ssh key及本地解决多个ssh key的问题
git生成ssh key及本地解决多个ssh key的问题 ssh是一种网络协议,用于计算机之间的加密登录.ssh原理及应用可参考: SSH原理与运用(一):远程登录 生成ssh key步骤 这里以配 ...
- JS监听事件错误:Uncaught TypeError: xx(函数名)is not a function at HTMLInputElement.onclick
事件监听一直出错,提示已定义的函数名不是一个函数,折腾了好久才想到,原来是函数名和JS内部关键字重名造成的. 以前也遇到过这种情况,但因为发生的概率比较小,就没太在意,但是这次感觉这方面确实需要注意, ...
- Python 之pdb调试
# 调试方式一:python -m pdb test.py # 相关命令: # l 查看当前代码 # n 向下执行一行代码 # c continue继续执行代码 # b break + 行数 添加断点 ...
- CWnd* pParent
Dlg(CWnd* pParent = NULL)的意思是:构造函数.创建对象时第一个调用的地方.CWnd* pParent=NULL是构造的参数,可以不传入,默认为NULL 构造函数(constru ...
- UpLoadify在IE下兼容问题
一.在IE9.IE10不能点击的问题解决 解决方法:进入uploadify的js文件中,搜索SWFUpload.prototype.getFlashHTML,找到它对应的语句,将方法全部替换为以下内容 ...
- 在Python中利用CVXOPT求解二次规划问题
工作中需要用到cvxopt,cvxopt安装有坑,大家注意下.1.首先一定要卸载numpy,无论是直接安装的,还是anaconda安装的,主要是必须用whl安装numpy才不会有包的冲突2.二次规划包 ...