题目:

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. React入门基础

    1-react概念: React是一个用于构建用户界面的JavaScript库.React主要用于构建UI,很多人认为React是MVC中的V(视图).React起源于Facebook的内部项目.Re ...

  2. javaScript 通过flie API读取本地文件

    File API是HTML5新增内容,依靠file和FileReader,这两个对象完成,代码如下: var fileInput = document.getElementById('test-ima ...

  3. Mysql 5.7 官方文档翻译

    始于 2017年4月1日-愚人节 1.1 MySQL 5.7 新功能 本章节介绍了MySQL 5.7 新版本中新增.废弃.删除的功能. 在1.5章节 Section 1.5, "Server ...

  4. 疯狂Java学习笔记(70)-----------挚爱Java

    与大家分享! 挚爱Java 10个使用Java最广泛的现实领域 写好Java代码的30条经验总结 Java字符串的substring真的会引起内存泄露么? Java内存的原型及工作原理深度剖析 Jav ...

  5. C++链接和执行相关错误

    http://blog.csdn.net/pipisorry/article/details/37610401 LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文 ...

  6. 设计模式入门之代理模式Proxy

    //代理模式定义:为其它对象提供一种代理以控制对这个对象的訪问 //实例:鉴于书中给出的样例不太好.并且有些疑问,所以直接用保护代理作为实例 //要求,一旦订单被创建,仅仅有订单的创建人才干够改动订单 ...

  7. 实战c++中的vector系列--再谈vector的insert()方法(都是make_move_iterator惹的祸)

    之前说过了关于vector的insert()方法,把vector B的元素插入到vector A中.vector A中的结果我们可想而知,可是vector B中的元素还会怎样? 看看之前写过的程序: ...

  8. 2016 ICPC CAMP Recording

    等了好久终于等到今天 马上能和群巨们一起学习了 希望不要暴露我太弱的本质............ 北京不冷,就是风大~~~ 1.24 8点准时起床了,准备下楼吃早饭 (这个宾馆好多美美的空姐对面就是东 ...

  9. bzoj1816: [Cqoi2010]扑克牌(二分答案判断)

    1816: [Cqoi2010]扑克牌 题目:传送门 题解: 被一道毒瘤题搞残了...弃了坑来刷刷水题 一开始还想复杂了...结果发现二分水过: 二分答案...然后check一下,joker肯定尽量用 ...

  10. 时间处理工具类TimeUtil

    转自:https://cnblogs.com/ityouknow/p/5662753.html 功能 Date与String之间的互相转换,以及一些特殊格式的时间字符串处理 代码 /** * 类名:T ...