给一个非负整数 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 各位相加的更多相关文章

  1. 258. Add Digits 数位相加到只剩一位数

    [抄题]: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...

  2. 258. Add Digits(C++)

    258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has ...

  3. 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 ...

  4. LN : leetcode 258 Add Digits

    lc 258 Add Digits lc 258 Add Digits Given a non-negative integer num, repeatedly add all its digits ...

  5. 【LeetCode】258. Add Digits (2 solutions)

    Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...

  6. LeetCode 258 Add Digits(数字相加,数字根)

    翻译 给定一个非负整型数字,反复相加其全部的数字直到最后的结果仅仅有一位数. 比如: 给定sum = 38,这个过程就像是:3 + 8 = 11.1 + 1 = 2.由于2仅仅有一位数.所以返回它. ...

  7. 258. Add Digits 入学考试:数位相加

    [抄题]: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...

  8. LeetCode 258. Add Digits

    Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...

  9. Java [Leetcode 258]Add Digits

    题目描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...

随机推荐

  1. IOS7状态栏StatusBar官方标准适配方法

    IOS7状态栏StatusBar官方标准适配方法 hello,大家好,ios7正式版已经发布,相信大家都在以各种方式来适配ios7. 如果你已经下载了xcode5,正准备使用,你会发现各种布局的改变. ...

  2. <a href="ip地址" target=""_blank">a里面的target</a>

    HTML <a> 标签的 target 属性 定义和用法 <a> 标签的 target 属性规定在何处打开链接文档. 如果在一个 <a> 标签内包含一个 targe ...

  3. poj——3687 Labeling Balls

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14835   Accepted: 4346 D ...

  4. mysql常用jar包

    连接Mysql数据库: 常用的连接池有两种 DBCP连接池 C3P0连接池 Apache的commons组件 -- DBCP连接池: commons-dbutils-1.4.jar 封装并简化了JDB ...

  5. vux picker

    1.通过实验证明: PopupPicker = TransferDom + Popup + PopupHeader + Picker 2.代码 Picker.vue <!-- Picker 组件 ...

  6. 工作总结 default Console.WriteLine(default(Guid));

    泛型代码中的默认关键字 在泛型类和泛型方法中产生的一个问题是,在预先未知以下情况时,如何将默认值分配给参数化类型 T: T 是引用类型还是值类型. 如果 T 为值类型,则它是数值还是结构. 给定参数化 ...

  7. gdb 调试利器

    1. gdb 调试利器 GDB是一个由GNU开源组织公布的.UNIX/LINUX操作系统下的.基于命令行的.功能强大的程序调试工具.对于一名Linux下工作的c++程序猿,gdb是不可缺少的工具: 1 ...

  8. ionic/cordova 真机调试

    android下简单,连接手机后,直接: myProjectPath>ionic run android ios下比较麻烦点,要先装ios-deploy: ​sudo npm install - ...

  9. 蓝桥 PREV-30 历届试题 波动数列 【动态规划】

      历届试题 波动数列   时间限制:1.0s   内存限制:256.0MB      问题描述 观察这个数列: 1 3 0 2 -1 1 -2 ... 这个数列中后一项总是比前一项增加2或者减少3. ...

  10. Ubuntu新版中javascript-common.conf害死人啊

    自动把javascript目录映射到系统的/usr/share 下面了,删掉对应的软链接再重启就好了