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 ...
随机推荐
- MongoDB小结06 - update【$push】
数组修改器,既然名字都这样叫了,那么这个修改器就只能对数组进行操作啦. db.user.update({"name":"qianjiahao"},{" ...
- 多button事件处理
private void ButtonClick(object sender, RoutedEventArgs e) { Button cmd = (Button)e.OriginalSource; ...
- 扩展gcd求解二元不定方程及其证明
#include <cstdio> #include <iostream> using namespace std; /*扩展gcd证明 由于当d = gcd(a,b)时: d ...
- lvm调整分区大小
1 问题 /home分区占用空间比较大,而/var分区比较小,它们位于同一个磁盘上.该系统安装了lvm. 2 减少/home分区空间 2.1 卸载/home umount /home 2.2 检查文件 ...
- JFreeChart自我总结
想飞就别怕摔 大爷的并TM骂人 JFreeChart自我总结 1.饼图.柱状图.折线图生成的工具类 1 package com.text.util; 2 3 import java.awt. ...
- VC/MFC列表CListCtrl类的LVCOLUMN和LVITEM详解
列表视图控件(List Control)列表视图控件是一种非常常用的控件,在需要以报表形式显示数据时,列表控件通常是最好的选择,许多专用的数据报表控件,也是在它的基础上派生而来.与树视图类似,列表 ...
- BZOJ_3790_神奇项链_manacher+贪心
BZOJ_3790_神奇项链_manacher+贪心 Description 母亲节就要到了,小 H 准备送给她一个特殊的项链.这个项链可以看作一个用小写字 母组成的字符串,每个小写字母表示一种颜色. ...
- 【单独编译使用WebRTC的音频处理模块 - android】
更新 [2015年2月15日] Bill 这段时间没有再关注 WebRTC 以及音频处理的相关信息,且我个人早已不再推荐单独编译 WebRTC 中的各个模块出来使用.实际上本文的参考价值已经很小了,甚 ...
- python 之filter()函数
filter()函数是 Python 内置的另一个有用的高阶函数,filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断,返回 True或 False,filt ...
- SVN报错 Not Found In Revision 不支持空目录
如果你要初始化上传的SVN目录为空,有可能会报这个错误 解决方法:在SVN下新建一个目录即可