寻找一个数的数根,用了暴力破解的方式,时间复杂度比较高

暂未想到O(1)的方式

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 res=10;
while(res>=10)
{
res=0;
while(num)
{
res=res+num%10;
num=num/10;
}
num=res;
}
return res;
}
};

12-Add Digits的更多相关文章

  1. 【LeetCode】Add Digits

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

  2. 258. Add Digits(C++)

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

  3. 【02_258】Add Digits

    Add Digits Total Accepted: 49702 Total Submissions: 104483 Difficulty: Easy Given a non-negative int ...

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

  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] Add Digits (a New question added)

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

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

  8. LeetCode:Add Digits - 非负整数各位相加

    1.题目名称 Add Digits (非负整数各位相加) 2.题目地址 https://leetcode.com/problems/add-digits/ 3.题目内容 英文:Given a non- ...

  9. LN : leetcode 258 Add Digits

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

  10. LeetCode_258. Add Digits

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

随机推荐

  1. alertmanager的使用

    alertmanager的使用 一.Alertanager的安装 1.下载 2.安装 3.启动 4.alertmanager和prometheus的整合 二.告警分组 1.告警规则 2.alertma ...

  2. 设计的MOS管三极管简单开关电路驱动能力不够1

    您需要 登录 才可以下载或查看,没有帐号?注册 x . ?& P' U5 r/ ~& `: B 用AOD409设计的开关电路为什么驱动能力不够,请大家帮忙分析一下原因啊.这个电路作用就 ...

  3. ip_local_port_range 和 ip_local_reserved_ports

    问题:启动应用程序时,发现网络端口被占用,原因是什么?如何避免? 原因:Linux 系统设置了随机使用的端口范围  echo "40000  60000" > /proc/. ...

  4. Balance的数学思想构造辅助函数

    本类题的证明难点便在于如何构造出满足答案的辅助函数.通过下列类题便可以很好的理解. 题一 该类题可以理解为积分内部f(x)与xf(x)处于一种失衡状态,故我们需要使其平衡,否则直接构造出的函数无法满足 ...

  5. gas-station leetcode C++

    There are N gas stations along a circular route, where the amount of gas at station i isgas[i]. You ...

  6. HTML js 页面倒计时后跳转至新页面

    HTML: 1 <body> 2 <p>操作错误!还有<span id="sp">5</span>秒跳转到交换机备份页面...< ...

  7. python教程-(四)当索引行不通时(python字典)

    一.创建和使用字典 1.函数dict #字典表示方式如下 >>> phonebook = {'tom':'18616271234',"Jim":"186 ...

  8. RedHat 7.0 下 FTP 服务的安装,启动,配置,以及虚拟用户的建立

    (注意! 区分shell命令和往配置文件里加的代码不同) 一:ftp服务的安装,启动和启用.   1:vim /etc/sysconfig/selinux     改为disabled后重启     ...

  9. 使用psftp向服务器上传文件

    老师刚才说想用psftp上传文件到服务器,我之前没听过,学了一下,总结下.我们用PSFTP主要也就是上传和下载. PSFTP是PuTTY SFTP客户端,用于本地与服务器间安全传输文件(使用SSH连接 ...

  10. JetBrains IntelliJ IDEA汉化

    JetBrains IntelliJ IDEA汉化 开启 IntelliJ IDEA,点击右下角Configure菜单,选择 Plugins.在弹出的 Plugins窗口里,切换至 Marketpla ...