题目:

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?

Hint:

  1. A naive implementation of the above process is trivial. Could you come up with other methods?

  2. What are all the possible results?
  3. How do they occur, periodically or randomly?
class Solution {
public:
int addDigits(int num) {
int res = 0;
bool doneFlag = false;
while(1){
res = 0;
while(num > 0){
res += num%10;
num /= 10;
}
if(res/10 == 0) break;
num = res;
}
return res;
}
};
class Solution {
public:
int addDigits(int num) {
if(num == 0) return 0;
if(num % 9 == 0) return 9;
return num%9;
}
};

[leetcode][math] Add Digits的更多相关文章

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

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

  2. 【LeetCode】Add Digits

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

  3. LN : leetcode 258 Add Digits

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

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

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

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

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

  6. LeetCode 258. Add Digits

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

  7. (easy)LeetCode 258.Add Digits

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

  8. Java [Leetcode 258]Add Digits

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

  9. LeetCode 258 Add Digits 解题报告

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

随机推荐

  1. 创业笔记-Node.js入门之基于事件驱动的回调

    基于事件驱动的回调 这个问题可不好回答(至少对我来说),不过这是Node.js原生的工作方式.它是事件驱动的,这也是它为什么这么快的原因. 你也许会想花点时间读一下Felix Geisendörfer ...

  2. nginx編譯

    openssl源码安装后,编译nginx-1.9.7或者openresty找不到OpenSSL的解决办法 http://blog.csdn.net/zhiyuan_2007/article/detai ...

  3. POJ 1306

    其实求的这个数的式子化简一下,就是C(N,M)..... #include <iostream> #include <algorithm> #include <cstdi ...

  4. 初识BeeFramework

    由于近期的项目须要,Hybrid开发成为我開始学习的新知识.非常早之前就了解到两个开发框架--BeeFramework 和 Samurai,可是由于本人一直没有闲暇去研究,所以就一直搁置一旁了.近期才 ...

  5. Android实战简易教程-第十三枪(五大布局研究)

    我们知道Android系统应用程序通常是由多个Activity组成,而这些Activity以视图的形式展如今我们面前, 视图都是由一个一个的组件构成的. 组件就是我们常见的Button.TextEdi ...

  6. 【c++版数据结构】之顺序表的实现

    SeqList.h #ifndef SEQLIST_H #define SEQLIST_H #include<iostream> using namespace std; typedef ...

  7. 关于sql中的with(nolock)

    SQL Server 中的 NOLOCK 究竟是什么意思 一般用于此类语句中:select * from t with(NOLOCK) nolock是不加锁查询.能够读取被事务锁定的数据,也称为脏读. ...

  8. HDFS HA架构以及源代码引导

    HA体系架构 相关知识介绍 HDFS master/slave架构,HDFS节点分为NameNode节点和DataNode节点. NameNode存有HDFS的元数据:主要由FSImage和EditL ...

  9. MYSQL binlog 日志内容查看

    记录mysql数据库真正执行更改的所有操作(DML语句),不包含那些没有修改任何数据的语句,不会记录select和show这样的语句. 二进制日志的作用: 1. 可以完成主从复制的功能 2. 进行恢复 ...

  10. MySQL学习(五)——使用JDBC完成用户表CRUD的操作

    通过案例我们发现“获得连接”和“释放资源”两次代码将在之后的增删改查所有功能中都存在,开发中遇到此种情况,将采用工具类的方法进行抽取,从而达到代码的重复利用. 1.使用properties配置文件 开 ...