Add Digits

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 = 111 + 1 = 2. Since 2 has only one digit, return it.

Follow up:
Could you do it without any loop/recursion in O(1) runtime?

解法一、按照定义做,直到结果只剩1位数字。

class Solution {
public:
int addDigits(int num) {
int n = num;
while(n > )
{
int cur = ;
while(n)
{
cur += (n % );
n /= ;
}
n = cur;
}
return n;
}
};

解法二、套公式digital root

class Solution {
public:
int addDigits(int num) {
return num - * floor((num - ) / );
}
};

【LeetCode】258. Add Digits (2 solutions)的更多相关文章

  1. 【LeetCode】 258. Add Digits 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:减1模9 方法三:直接模9 日 ...

  2. 【LeetCode】258. Add Digits

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

  3. 【一天一道LeetCode】#258. Add Digits

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【LeetCode】623. Add One Row to Tree 解题报告(Python)

    [LeetCode]623. Add One Row to Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problem ...

  5. 【leetcode】1291. Sequential Digits

    题目如下: An integer has sequential digits if and only if each digit in the number is one more than the ...

  6. 【LeetCode】415. Add Strings 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...

  7. 【LeetCode】989. Add to Array-Form of Integer 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组转整数再转数组 模拟加法 日期 题目地址:htt ...

  8. 【LeetCode】75. Sort Colors (3 solutions)

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  9. 【LeetCode】90. Subsets II (2 solutions)

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

随机推荐

  1. html5 文件上传 带进度条

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  2. web大文件上传控件-jsp-oracle-bug修复-Xproer.HttpUploader6

    说明: 修复删除文件夹逻辑错误的问题   1.更新up6.js 更新前: 更新后: 代码:         , "RemoveFolder": function (fileSvr) ...

  3. kettle etl

    使用注意点 1 如果服务器资源有限的话,尽量少开任务窗口,但是要有容错机制,可以分为按天按分钟 2 如果不想写较长的sql可以用detail来启动 3 在设置每天提交的条数时,如果数据很少,而设置值很 ...

  4. 1.4 Service

    用于在后台完成用户指定的操作,为其他组件提供后台服务或监控其他组件的运行状态. 开发人员需要在应用程序配置文件中声明全部的service,使用<service></service&g ...

  5. php笔试题(1)--转载

    一份不错的php面试题,附答案,有准备换工作的同学可以参考一下.一.基础题1. 写出如下程序的输出结果 <?php     $str1 = null;     $str2 = false;    ...

  6. 视图组件(View)

    1.Android应用的大部分UI组件都放在了android.widget包及其子包,android.view包及其子包中,Android应用的所有UI组件都继承了View类2.View类还有一个重要 ...

  7. JSON与XML的区别

    1.定义介绍 (1).XML定义扩展标记语言 (Extensible Markup Language, XML) ,用于标记电子文件使其具有结构性的标记语言,可以用来标记数据.定义数据类型,是一种允许 ...

  8. 尝试在mac上用dotnet cli运行asp.net core示例程序

    自从知道微软用dotnet cli取代dnx之后,一直在等dotnet cli支持asp.net core... 昨天看到这篇新闻(ASP.NET Core 1.0 Hello World)后,才知道 ...

  9. MyEclipse 8.5 优化实例

    在用[MyEclipse] 写代码很容易卡死机,尤其是在对JSP文件的<%%>之间写代码的时候,只要一弹出智能提示就立刻卡死,程序失去响应,我以为是MyEclipse版本的问题,结果换了6 ...

  10. Visual Studio 发布新版API智能提示

    Visual Studio 新版API智能提示两周前发布.有了它,你可以在调用API的同时,方便了解到API的相关示例代码.这大大地有助于开发人员学习和使用API. 安装方法如下: 1. 打开Visu ...