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 ...
随机推荐
- 洛谷 P4057 [Code+#1]晨跑
P4057 [Code+#1]晨跑 题目描述 “无体育,不清华”.“每天锻炼一小时,健康工作五十年,幸福生活一辈子” 在清华,体育运动绝对是同学们生活中不可或缺的一部分.为了响应学校的号召,模范好学生 ...
- java开发中涉及到的调优
JVM内存的调优 默认的Java虚拟机的大小比较小,在对大数据进行处理时java就会报错:java.lang.OutOfMemoryError. 1. Heap设定与垃圾回收Java Heap分为3个 ...
- Redis集群方案收集
说明: 如果不考虑客户端分片去实现集群,那么市面上基本可以说就三种方案最成熟,它们分别如下所示: 系统 贡献者 是否官方Redis实现 编程语言 Twemproxy Twitter 是 C Redis ...
- Java发送邮件示例
利用Java发送邮件示例: 1.发送QQ邮件 import java.util.Properties; import javax.mail.Message; import javax.mail.Mes ...
- WebLogic(12C)——简单公布和JDBC
一,简单公布应用 1.点击"安装".開始部署应用: 2,找到要部署的项目路径: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGFue ...
- 配置-XX:+HeapDumpOnOutOfMemoryError 对于OOM错误自动输出dump文件
配置-XX:+HeapDumpOnOutOfMemoryError 对于OOM错误自动输出dump文件 学习了:http://blog.csdn.net/stevendbaguo/article/de ...
- Linux挂载新盘
Linux 系统挂载数据盘 1.查看数据盘 使用“fdisk-l”命令查看 2. 对数据盘进行分区 执行“fdisk /dev/sdb”命令,对数据盘进行分区: 输入“n”,“p”“1”,两次回车,“ ...
- 蓝桥杯 地宫寻宝 带缓存的DFS
历届试题 地宫取宝 时间限制:1.0s 内存限制:256.0MB 问题描写叙述 X 国王有一个地宫宝库. 是 n x m 个格子的矩阵. 每一个格子放一件宝贝. 每一个宝贝贴着价 ...
- andriod socket开发问题小结
andriod socket开发问题小结 个人信息:就读于燕大本科软件project专业 眼下大四; 本人博客:google搜索"cqs_2012"就可以; 个人爱好:酷爱数据结构 ...
- HTML5+CSS3设计界面
近期在做一个关于房屋装修的手机上的项目,前台是用H5+C3完毕的,挂在微信上.全部相对来说不是非常难. 这段时间通过敲Html5+Css3.分享一些自己觉得值得学习的知识. 都非常easy.自己操作一 ...