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 ...
随机推荐
- 高数(A)下 第十二章
12.1 12.2 12.3 12.4 12.5 12.6 自测题
- Spring 加载类路径外的资源文件
原文:http://blog.csdn.net/gaofuqi/article/details/46417259 <bean id="propertyConfigurer" ...
- AE After Effect 渲染如何输出设置
各种输出设置值的对比情况. Microsoft Video1压缩方法情况(该模式下无法采用RGB+Alpha): 一 深度为"数千种颜色",缩放为1280×720(HDV/HDTV ...
- 如何在其他js 引入main.js 中 vue 的实例?
1.原因解析 经测试发现,代码先执行了 index.js >> main.js >> Home.vue scr/api/index.js src/main.js src/co ...
- mysql连接字符串,连接字段结果集
archie2010 ${原来姹紫嫣红开遍,似这般都付与扣钉八哥} mysql连接字符串,连接字段结果集 select CONCAT('My', 'S', 'QL连接字符串') as MySql; 连 ...
- 秒懂C#通过Emit动态生成代码 C#使用Emit构造拦截器动态代理类
秒懂C#通过Emit动态生成代码 首先需要声明一个程序集名称, 1 // specify a new assembly name 2 var assemblyName = new Assembly ...
- PAT Rational Arithmetic (20)
题目描写叙述 For two rational numbers, your task is to implement the basic arithmetics, that is, to calcul ...
- 进程间通信之-共享内存Shared Memory--linux内核剖析(十一)
共享内存 共享内存是进程间通信中最简单的方式之中的一个. 共享内存是系统出于多个进程之间通讯的考虑,而预留的的一块内存区. 共享内存同意两个或很多其他进程訪问同一块内存,就如同 malloc() 函数 ...
- 下载的php_yal.dll文件添加到php的ext
下载的php_yal.dll文件添加到php的ext http://pecl.php.net/package/yaf/3.0.6/windows yaf. use_namespace=1 ;;exte ...
- 【POI 2010】 Pilots
[题目链接] 点击打开链接 [算法] 单调队列 [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 3000010 ...