Problem:

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?

此题最初想法是相加、判断反复循环直到找到满足的解。通过参考别人的解答发现,此题是让我们求“数根”。

由此解法一为通过循环相加判断,直到找到可行解:

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

但是由于时间复杂度超过题目要求,故考虑针对“数根”的解法。

通过枚举可知:   
10 1+0 = 1
11 1+1 = 2
12 1+2 = 3    
13 1+3 = 4
14 1+4 = 5
15 1+5 = 6
16 1+6 = 7
17 1+7 = 8
18 1+8 = 9
19 1+9 = 1
20 2+0 = 2

于是可得出规律,每9个一循环。为处理9的倍数的情况,我们写为(n - 1) % 9 + 1。由此可得:

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

LeetCode 258. Add Digits的更多相关文章

  1. LN : leetcode 258 Add Digits

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

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

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

  3. [LeetCode] 258. Add Digits 加数字

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

  4. (easy)LeetCode 258.Add Digits

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

  5. Java [Leetcode 258]Add Digits

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

  6. LeetCode 258 Add Digits 解题报告

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

  7. leetcode 258. Add Digits(数论)

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

  8. LeetCode: 258 Add Digits(easy)

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

  9. leetcode 258. Add Digits——我擦,这种要你O(1)时间搞定的必然是观察规律,总结一个公式哇

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

随机推荐

  1. XML基础

    什么是XML? XML(Extensible Markup Language)可扩展标记语言,是一种以简单文本格式存储数据的方式. 它最重要的组成部分是XML元素,包含了文档的实际数据. XML的几个 ...

  2. ng-disabled 不起作用的解决办法

    不知道这算不算 Angular.js 的一个bug.但搜索一番后找到了一个变通的解决办法. 业务需求是这样的, 按钮被点击一次之后就设置为禁用状态, 以阻止多次无效的点击.但现在很多框架都用 < ...

  3. height:100%与height:inherit的区别

    一.兼容性 首先,inherit这个属性只是在ie8+才支持:100%支持ie6: 二.大多数情况下没有区别 在正常情况下height:100%与height:inherit没有任何区别: 1.父元素 ...

  4. 详解SpringMVC中GET请求

    GET请求概述 GET请求,请求的数据会附加在URL之后,以?分割URL和传输数据,多个参数用&连接.URL的编码格式采用的是ASCII编码,而不是uniclde,所有的非ASCII字符都要编 ...

  5. Visual Studio 常用快捷键备忘

    在代码中插入书签 用途 操作   vs2013 快速在自定义的不同代码位置跳转 首先点击: 编辑=>书签=>启用书签 然后再在代码编辑窗口 ctrl+k, k (取消书签,再按一次 ctr ...

  6. Python全栈开发【面向对象进阶】

    Python全栈开发[面向对象进阶] 本节内容: isinstance(obj,cls)和issubclass(sub,super) 反射 __setattr__,__delattr__,__geta ...

  7. awk

    cat map-matcher.log | awk -F '[' '{print $1}' | awk -F '-' '{print $2}' >result.txt cat 2.txt | a ...

  8. bootstrap-select js jQuery控制select属性变化

    bootstrap-select我想大家都不陌生是一个前端下拉框的插件非常好用,在select的标签中设置属性可以做很多功能控制,不过初始化之后怎么去修改网上找遍中文英文也没有一个交代自己研究好久研究 ...

  9. APM程序分析-Control_rtl.cpp

    bool Copter::rtl_init(bool ignore_checks) { if (position_ok() || ignore_checks) { rtl_build_path(!fa ...

  10. Centos7下安装mysql5.7.16

    mysql的安装(root用户下) 从官网下载软件 linux下必须安装系统对应的版本,多少位 必须安装的是:server,client 但是我可不管要安装那个插件,我们直接使用bundle版本(就是 ...