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. Mysql的学习研究

    2017年5月16日11:26:17 从今天开始过一遍数据库的基础教程,加油!!!!! 看了之后对一些基础知识有了理解,加油... 笔记: 2017年5月16日11:35:46mysql的基础教程1. ...

  2. 你须知道的30个CSS选择器 »

    你也许已经掌握了id.class.后台选择器这些基本的css选择器.但这远远不是css的全部.下面向大家系统的解析css中30个最常用的选择器,包括我们最头痛的浏览器兼容性问题.掌握了它们,才能真正领 ...

  3. spring boot ajax post 前后端

    1 传输的数据格式是json 1.1 前端ajax json的所有的key都必须是双引号引用的,并且最外层也要用双引号引用.例如 "{"a":b, "b&quo ...

  4. React-Native 安装改变镜像

    3.安装完node后建议设置npm镜像以加速后面的过程,否则后面插件安装巨慢 npm config set registry https://registry.npm.taobao.org --glo ...

  5. CentOS 7.0 systemd

    CentOS 7 已经切换到 systemd,系统指令也有所变化.之前用于启动.重启.停止各种服务的service 作为向后兼容的指令还能使用,但是将来可能会消失.同时,chkconfig 也改成了s ...

  6. ABAP 面向对象(Object Orientation) OO

    [转自 http://blog.sina.com.cn/s/blog_7c7b16000101bhof.html]在程序中, 对象的识别和寻址是通过对象引用来实现的, 对象引用变量可以访问对象的属性和 ...

  7. (转)Spring 缓存EhCacheFactoryBean

    Spring使用Cache 从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我 ...

  8. 有关java之反射的使用

    1 public class Demo02 { 2 @SuppressWarnings("all") 3 public static void main(String[] args ...

  9. Elasticsearch for python API模块化封装

    Elasticsearch for python API模块化封装 模块的具体功能 检测Elasticsearch节点是否畅通 查询Elasticsearch节点健康状态 查询包含的关键字的日志(展示 ...

  10. oc中的blocks的功能,一种比代理简洁的方式

    blocks方式: 谁要东西谁就要写blocks,通过blocks(返回值)获取想要的,提供东西的人要通过实现函数指针把东西给想要的人.当然也可以传值,传值需要通过形参. block的功能: bloc ...