1、题目名称

Add Digits (非负整数各位相加)

2、题目地址

https://leetcode.com/problems/add-digits/

3、题目内容

英文:Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

中文:有一个非负整数num,重复这样的操作:对该数字的各位数字求和,对这个和的各位数字再求和……直到最后得到一个仅1位的数字(即小于10的数字)。

例如:num=38,3+8=11,1+1=2。因为2小于10,因此返回2。

4、解题方法

 class Solution {
public:
int addDigits(int num) {
if(num/==) return num;
int middle=;
while(num>=){
middle=middle+num%;
num=num/;
}
return addDigits(middle+num); }
};

LeetCode:Add Digits - 非负整数各位相加的更多相关文章

  1. [LeetCode258] Add Digits 非负整数各位相加

    题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...

  2. [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. ...

  3. LeetCode 258 Add Digits(数字相加,数字根)

    翻译 给定一个非负整型数字,反复相加其全部的数字直到最后的结果仅仅有一位数. 比如: 给定sum = 38,这个过程就像是:3 + 8 = 11.1 + 1 = 2.由于2仅仅有一位数.所以返回它. ...

  4. LeetCode OJ:Add Digits(数字相加)

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  5. [LeetCode] Add Digits 加数字

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  6. (leetcode)Add Digits

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  7. LeetCode——Add Digits

    Description: Given a non-negative integer num, repeatedly add all its digits until the result has on ...

  8. LeetCode Add Digits (规律题)

    题意: 将一个整数num变成它的所有十进制位的和,重复操作,直到num的位数为1,返回num. 思路: 注意到答案的范围是在区间[0,9]的自然数,而仅当num=0才可能答案为0. 规律在于随着所给自 ...

  9. LeetCode 258. 各位相加(Add Digits)

    258. 各位相加 258. Add Digits 题目描述 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. LeetCode258. Add Digits 示例: 输入: 3 ...

随机推荐

  1. docker安装并配置加速

    安装 旧版本的 Docker 称为 docker 或者 docker-engine,使用以下命令卸载旧版本: sudo apt-get remove docker \ docker-engine \ ...

  2. js跨浏览器复制: ZeroClipboard

    实例结构: demo.html <script type="text/javascript" src='http://code.jquery.com/jquery.js'&g ...

  3. lua面向对象封装

    lua面向对象的一个封装,直接贴代码 --swfclass = {};local cs = {};function _class( child, base, ... )--    _.s( child ...

  4. BZOJ1505: [NOI2004]小H的小屋

    BZOJ1505: [NOI2004]小H的小屋 Description 小H发誓要做21世纪最伟大的数学家.他认为,做数学家与做歌星一样,第一步要作好包装,不然本事再大也推不出去. 为此他决定先在自 ...

  5. 我的Android进阶之旅------>android api的源代码下载地址

    Eclipse的SDK Manager不好用,关联不到API的源代码,终于找到一个可以下载API对应的source地址: http://grepcode.com/project/repository. ...

  6. php7下 xhprof安装与使用

    需要测试下 代码的性能,使用了 xhprof + xhgui 1. 下载xhprof, 这里下载吧 :https://github.com/longxinH/xhprof.git 2, 安装 cd x ...

  7. QMessageBox简单使用

    首先要调用 #include <QMessageBox> 然后 QMessageBox msgBox; msgBox.setWindowTitle("错误"); msg ...

  8. Android中子线程真的不能更新UI吗?

    Android的UI访问是没有加锁的,这样在多个线程访问UI是不安全的.所以Android中规定只能在UI线程中访问UI. 但是有没有极端的情况?使得我们在子线程中访问UI也可以使程序跑起来呢?接下来 ...

  9. 牛客小白月赛1 H 写真がとどいています 【循环】

    题目链接 https://www.nowcoder.com/acm/contest/85/H 思路 如果熟悉 五线谱 才能做啊... 然后 先竖着遍历 再 横着 遍历 就可以了 AC代码 #inclu ...

  10. [转] 携程App网络服务通道治理和性能优化@2016

    App网络服务的高可靠和低延迟对于无线业务稳定发展至关重要,过去两年来我们一直在持续优化App网络服务的性能,到今年Q2结束时基本完成了App网络服务通道治理和性能优化的阶段性目标,特此撰文总结其中的 ...