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

For example:

Given num = 38, the process is like: 3
+ 8 = 11
1 + 1 = 2. Since 2 has
only one digit, return it.

实现:

class Solution {

public:

    int addDigits(int num) {

        int result = num;

        while (true) {

            if (result < 10) {

                return result;

            }

            num = result;

            result = 0;

            while (num) {

                result = result + num % 10;

                num = num / 10;

            }

        }

    }

};

LeetCode258——Add Digits的更多相关文章

  1. LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number

    数学题 172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. N ...

  2. LeetCode258:Add Digits

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

  3. [LeetCode258] Add Digits 非负整数各位相加

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

  4. LeetCode 258. 各位相加(Add Digits)

    258. 各位相加 258. Add Digits 题目描述 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. LeetCode258. Add Digits 示例: 输入: 3 ...

  5. 【LeetCode】Add Digits

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

  6. Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference

    最近做的题记录下. 258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the ...

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

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

  8. [LeetCode] Add Digits (a New question added)

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

  9. 258. Add Digits(C++)

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

随机推荐

  1. [Apple开发者帐户帮助]九、参考(3)支持的功能(iOS)

    iOS应用程序可用的功能取决于您的程序成员身份. 能力 ADP 企业 Apple开发者 访问Wifi信息   应用程序组 Apple Pay     相关域名   自动填充凭据提供程序   背景模式 ...

  2. 慕课网6-2 作业:js实现轮播特效

    小伙伴们,掌握了JavaScript的语法.流程控制语句.内置对象以及DOM和BOM的知识,运用所学知识完成如下图所示的交互效果——轮播图.效果图如下: 具体交互效果图参考gif动态效果图,gif效果 ...

  3. 【BZOJ3328】PYXFIB(数学)

    什么都不会的数学蒻菜瑟瑟发抖--Orz橙子(和兔子) 题目: BZOJ3328 分析: 橙子给我安利的数学题--(然后我就看着他因为矩阵乘法多模了一次卡了一天常数qwq表示同情) 先考虑一个子问题:求 ...

  4. 浅谈Java中的hashcode方法以及equals方法

    哈希表这个数据结构想必大多数人都不陌生,而且在很多地方都会利用到hash表来提高查找效率.在Java的Object类中有一个方法: public native int hashCode(); 根据这个 ...

  5. 给网站添加运行时间的JavaScript完整代码

    function secondToDate(second) { if (!second) { return 0; } var time = new Array(0, 0, 0, 0, 0); if ( ...

  6. Android热修复方案比较

    热修复的特点:无需重新发版,实时高效热修复:用户无感知修复,无需下载新的应用,代价小: 修复成功率高,把损失降到最低. 一.热修复开源方案和使用情况 方案名称 方案开发公司 开发时间 Github星评 ...

  7. JS——思维拓展

    1.阶乘求和:4的阶乘是1*2*3*4 <script> function jiechen(value) { var n = 1; for (var i = 1; i <= valu ...

  8. Caffe2:添加CUDA路径

    在使用Eclipse时候,仍然出现 libcuda.so 找不到的情况..... 原因:cuda环境没有设置到系统环境中. 方案: 设置CUDA path: 在bashrc中添加路径,只对当前用户产生 ...

  9. graphite 绘图工具

    graphite 绘图工具

  10. jstree CHECKBOX PLUGIN

    The checkbox plugin makes multiselection possible using three-state checkboxes. Configuration overri ...