LeetCode:Add Digits - 非负整数各位相加
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 - 非负整数各位相加的更多相关文章
- [LeetCode258] Add Digits 非负整数各位相加
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
- [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. ...
- LeetCode 258 Add Digits(数字相加,数字根)
翻译 给定一个非负整型数字,反复相加其全部的数字直到最后的结果仅仅有一位数. 比如: 给定sum = 38,这个过程就像是:3 + 8 = 11.1 + 1 = 2.由于2仅仅有一位数.所以返回它. ...
- LeetCode OJ:Add Digits(数字相加)
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- [LeetCode] Add Digits 加数字
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- (leetcode)Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- LeetCode——Add Digits
Description: Given a non-negative integer num, repeatedly add all its digits until the result has on ...
- LeetCode Add Digits (规律题)
题意: 将一个整数num变成它的所有十进制位的和,重复操作,直到num的位数为1,返回num. 思路: 注意到答案的范围是在区间[0,9]的自然数,而仅当num=0才可能答案为0. 规律在于随着所给自 ...
- LeetCode 258. 各位相加(Add Digits)
258. 各位相加 258. Add Digits 题目描述 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. LeetCode258. Add Digits 示例: 输入: 3 ...
随机推荐
- ajax 和jsonp 不是一码事 细读详解
由于Sencha Touch 2这种开发模式的特性,基本决定了它原生的数据交互行为几乎只能通过AJAX来实现. 当然了,通过调用强大的PhoneGap插件然后打包,你可以实现100%的Socket通讯 ...
- SELinux状态修改
查看SELinux状态: 1./usr/sbin/sestatus -v ##如果SELinux status参数为enabled即为开启状态 SELinux status: ...
- SpringMVC的第一个入门案例
用户提交一个请求,服务器端处理器在接收到这个请求后,给出一条欢迎信息,在页面中显示. 第一步:导入jar包 在原有Springjar包基础上添加2个jar包 spring-webmvc-4.2.0.R ...
- linux sort按照指定列排序
sort怎样按指定的列排序0000 27189 41925425065f 15 419254250663 7 419254250675 5 419254250691 76 419254250693 2 ...
- iOS category 类别 和 extension 扩展
category 类别 又称为 分类 在ios项目开发中允许使用类别为现有的类添加新的方法,并不需要创建子类.通过类别我们可以动态地为现有的类添加新的方法,可以将类的定义模块化地布局到多个相关文件中 ...
- Mysql——JDBC编程 理论介绍
一.JDBC简介(来自俞琰--数据库老师) Java数据库编程主要使用JDBC技术.JDBC是一种用于执行SQL语句的Java API.它由一组用Java编写的类和接口组成.JDBC为开发人员提供了一 ...
- 文字溢出 省略css
overflow:hidden; text-overflow:ellipsis; -o-text-overflow:ellipsis; white-space:nowrap;
- img标签中alt属性与title属性
alt属性 1.alt属性是考虑到不支持图像显示或者图像显示被关闭的浏览器的用户,以及视觉障碍的用户和使用屏幕阅读器的用户.当图片不显示的时候,图片的替换文字.2.alt属性值得长度必须少于100个英 ...
- JavaScript日期选择控件Kalendae
在线演示 本地下载
- java深入探究01
经过前面基础部门的学习,希望大家都把基础打闹再继续深入探究java应用层面的知识,以后的日子我会继续更新java进阶知识,深入探究实际工作中的java应用,说的不好的地方还请见谅,如果能提出你宝贵的建 ...