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

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

题目大意:

给定一个非负整数num,重复地将其每位数字相加,直到结果只有一位数为止。

例如:

给定 num = 38,过程像这样:3 + 8 = 11, 1 + 1 = 2。因为2只有一位,返回之。

进一步思考:

你可以不用循环,在O(1)运行时间内完成题目吗?

解题方法:

1.模拟上述方法(递归)。

2.观察法。

使用方法I的代码循环输出0 - 19的运行结果:

in  out  in  out
0 0 10 1
1 1 11 2
2 2 12 3
3 3 13 4
4 4 14 5
5 5 15 6
6 6 16 7
7 7 17 8
8 8 18 9
9 9 19 1

可以发现输出与输入的关系为:

out = (in - 1) % 9 + 1

注意事项:

C++代码:

1.递归:

 class Solution
{
public:
int addDigits(int num)
{
int tmp=num/+num%;
if(tmp<)
{
return tmp;
}
else
{
num=tmp;
tmp=addDigits(num);
}
return tmp;
}
};

2.观察法:

 class Solution
{
public:
int addDigits(int num)
{
if (num == )
return ;
return (num - ) % + ;
}
};

258. Add Digits(C++)的更多相关文章

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

  2. LN : leetcode 258 Add Digits

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

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

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

  4. LeetCode 258. Add Digits

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

  5. (easy)LeetCode 258.Add Digits

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

  6. 258. Add Digits

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

  7. Java [Leetcode 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

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

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

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

随机推荐

  1. bzoj3209

    首先这道题目不难想到将答案转化为这种形式 2^s[2]*3*s[3]*…max*s[max] 这时候我们要分类讨论,设n的二进制位数为t 当1~n中二进制位数小于t时 我们可以直接用组合的知识,二进制 ...

  2. Android 布局之DrawLayout

    在刚开始学android的时候肯定会知道,android的主要的布局就是LinearLayout.RelativeLayout.FramLayout.AbsoluteLayout.以及TableLay ...

  3. Windows下安装和配置Maven的方法及注意事项

    首先,从http://maven.apache.org/download.cgi网站上下载适用于当前操作系统操作格式的最新版本的maven安装包.如:apache-maven-3.2.5-bin.zi ...

  4. 内存模型(memory models)和命名空间(namespace)

    继续<C++ premier plus > 先来解释一下scope和linkage,所谓scope,是指变量的作用范围,所谓linkage,是指变量能否在不同文件中共享 1,自动变量(au ...

  5. java IO复习(二)

    package com.zyw.file; import java.io.*; /** * Created by zyw on 2016/3/10. */ public class FileTest2 ...

  6. sqlplus乱码

    使用SecureCRT或是pietty_ch连接到一台安装有Oracle DB 10g的RHEL4.2的机器,linux使用的shell是默认的bash. 在bash提示符下,使用Del键或者Back ...

  7. Debug调试

    1.F5单步调试进入函数内部 2.F6单步调试进行下一步 3.F7由函数内部返回到调用用处 4.F8一直执行到下一个断点

  8. PDO 提供了三种不同的错误处理模式

    PDO::ERRMODE_SILENT 此为默认模式. PDO 将只简单地设置错误码,可使用 PDO::errorCode() 和 PDO::errorInfo() 方法来检查语句和数据库对象.如果错 ...

  9. 面试题 php随机获取概率结果

    题目:随机输出“苹果”,“橘子”,“香蕉”要求输出“苹果”的概率为50%,“橘子”的概率为30%,“香蕉”的概率为20% 分析 方案一: 最常用rand(1,10)来处理 如果是5以下的输出苹果 6到 ...

  10. Media Queries 自适应布局展示

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...