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 = 111 + 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?
    4. You may find this Wikipedia article useful.
      int addDigits(int num)
      {
      return num-(num-)/*;
      }

LeetCode258:Add Digits的更多相关文章

  1. LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number

    数学题 172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. N ...

  2. [LeetCode258] Add Digits 非负整数各位相加

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

  3. LeetCode258——Add Digits

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

  4. LeetCode 258. 各位相加(Add Digits)

    258. 各位相加 258. Add Digits 题目描述 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. LeetCode258. Add Digits 示例: 输入: 3 ...

  5. 【LeetCode】Add Digits

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

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

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

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

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

  9. 258. Add Digits(C++)

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

随机推荐

  1. HDU 3544 (不平等博弈) Alice's Game

    切巧克力的游戏,想得还是不是太明白. 后者会尽量选前着切后其中小的一块来切,那么先手须尽量取中间来切. So?题解都是这么一句话,不知道是真懂了还是从别人那抄过来的. 后来找到一个官方题解,分析得比较 ...

  2. [Warning] TIMESTAMP with implicit DEFAULT value is deprecated

    As indicated by the warning, to turn off the nonstandard behaviors, enable the new explicit_defaults ...

  3. codevs 4919 线段树练习4

    线段树水题.我是ziliuziliu,我是最强的#include<iostream> #include<cstdio> #include<cstring> #inc ...

  4. PHP设计模式之装饰者模式

    <?php /* 装饰者模式动态地将责任附加到对象上.若要扩展功能,装饰者提供了比继承更有弹性的替代方案. */ header("Content-type:text/html; cha ...

  5. Mysql管理工具SQLyog

    SQLyog_Enterprise 用户名:yunjian 注册码:81f43d3dd20872b6   http://download.csdn.net/detail/shel_lee/585361 ...

  6. S2sh整合MAven项目所需坐标大全

    <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> & ...

  7. 【英语】Bingo口语笔记(9) - 表示“不相信”

  8. linux 命令部分说明

    shell 文件头格式   #! /bin/sh 定义变量  dir_tmp=/tmp/xxx 级联创建 mkdir -p /etc/aaa/bbb 阻塞命令 等待用户输入回车  继续    read ...

  9. linux的命令(1)

    系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...

  10. 【转】Core Bluetooth框架之一:Central与Peripheral

    原文网址:http://southpeak.github.io/blog/2014/07/29/core-bluetoothkuang-jia-zhi-%5B%3F%5D-:centralyu-per ...