258 Add Digits 各位相加
给一个非负整数 num,反复添加所有的数字,直到结果只有一个数字。
例如:
设定 num = 38,过程就像: 3 + 8 = 11, 1 + 1 = 2。 由于 2 只有1个数字,所以返回它。
进阶:
你可以不用任何的循环或者递归算法,在 O(1) 的时间内解决这个问题么?
详见:https://leetcode.com/problems/add-digits/description/
Java实现:
class Solution {
public int addDigits(int num) {
while(num/10>0){
int sum=0;
while(num>0){
sum+=num%10;
num/=10;
}
num=sum;
}
return num;
}
}
方法二:
class Solution {
public int addDigits(int num) {
return (num-1)%9+1;
}
}
C++实现:
方法一:
class Solution {
public:
int addDigits(int num) {
while(num/10>0)
{
int sum=0;
while(num>0)
{
sum+=num%10;
num/=10;
}
num=sum;
}
return num;
}
};
方法二:
class Solution {
public:
int addDigits(int num) {
return (num-1)%9+1;
}
};
参考:https://www.cnblogs.com/grandyang/p/4741028.html
258 Add Digits 各位相加的更多相关文章
- 258. Add Digits 数位相加到只剩一位数
[抄题]: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...
- 258. Add Digits(C++)
258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has ...
- 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(数字相加,数字根)
翻译 给定一个非负整型数字,反复相加其全部的数字直到最后的结果仅仅有一位数. 比如: 给定sum = 38,这个过程就像是:3 + 8 = 11.1 + 1 = 2.由于2仅仅有一位数.所以返回它. ...
- 258. Add Digits 入学考试:数位相加
[抄题]: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...
- LeetCode 258. Add Digits
Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...
- Java [Leetcode 258]Add Digits
题目描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...
随机推荐
- linux 常见名词及命令(四)
yum仓库的配置 yum仓库的配置文件存放在/etc/yum.repos.d/目录中. 第一步:切换到/etc/yum.repos.d/目录中. 第二步:使用vim编辑器打开一个名为'rhel7.re ...
- Toast自定义
Toast toast=new Toast(MainActivity.this); toast.setView(getLayoutInflater().inflate(R.layout.toast,n ...
- 高数(A)下 第十二章
12.1 12.2 12.3 12.4 12.5 12.6 自测题
- 作为一名CEO
你 不能怕得罪人 不能奢望完成工作的时候 有太多的愉悦感 你 必须要去做左右为难但左右亦可的 操蛋决策 你 得脸皮够厚 肚囊儿宽超 什么事情都能快速消化 哪怕 是 一坨屎 你 还得 决不放弃 永不言败 ...
- mvn打包源码的方法:maven-source-plugin
maven-javadoc-plugin可以打包 dubbo-demo-provider-2.6.1-javadoc.jar maven-jar-plugin 打包插件 dubbo-demo-prov ...
- javascript 时间格式(很方便的原生函数)
原文:http://www.cnblogs.com/yjf512/p/3796229.html --------------------- <html xmlns="http://ww ...
- Num 15: NYOJ: 题目0002 : 括号配对问题 [ 栈(stack) ]
原题连接 首先要了解有关栈的一些基本知识,即: 什么是栈,栈有什么作用: 1.什么是栈: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkb ...
- grep 并列查询 效率 且 或
find / | grep -v python | grep -v xl_ | grep -v xiaole |grep redis [root@hadoop3 ~]# find / | grep - ...
- CSP 201612-3 权限查询 【模拟+STL】
201612-3 试题名称: 权限查询 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 授权 (authorization) 是各类业务系统不可缺少的组成部分,系统用户通过授权 ...
- SemaphoreSlim
https://msdn.microsoft.com/en-us/library/system.threading.semaphoreslim(v=vs.110).aspx Represents a ...