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. 连接mongo的服务提示:HTTP Status 500 - com.mongodb.MongoException$Network: can't call something

    连接mongo的服务提示以下错误 原因:达到了mongodb启动时预设的最大连接数,无法创建新的连接 HTTP Status 500 - com.mongodb.MongoException$Netw ...

  2. 【英语】Bingo口语笔记(74) - put系列

  3. 解决VS2013/VS2010简体中文版无法使用ReSharper快捷键的问题

    对于简体中文版VS2013,若应用ReSharper快捷键失效,按下面方法修复: 1) 首先在ReSharper设置里,应用你的快捷键设置 2) 关闭VS2013,然后用记事本打开“%LOCALAPP ...

  4. 剑指offer-第三章高质量的代码(调整数组顺序使得奇数位于偶数的前面)

    题目:输入一个整数数组,实现一个函数中调整该数组中数字的顺序,使得所有的奇数位于数组的前半部,所有偶数位于数组的后半部. 思路:用两个指针p1和p2,分别指向数组的头和尾部,p1只向后移,p2只向前移 ...

  5. hadoop完全分布式模式的安装和配置

    本文是将三台电脑用路由器搭建本地局域网,系统为centos6.5,已经实验验证,搭建成功. 一.设置静态IP&修改主机名&关闭防火墙(all-root)(对三台电脑都需要进行操作) 0 ...

  6. 五个JS经典面试题

    1:Scope作用范围 1: (function() { 2: var a = b = 5; 3: })(); 4: 5: console.log(b); 什么会被打印在控制台上? 回答 上面的代码会 ...

  7. 使用 foreach 操作数组

    foreach 并不是 Java 中的关键字,是 for 语句的特殊简化版本,在遍历数组.集合时, foreach 更简单便捷.从英文字面意思理解 foreach 也就是“ for 每一个”的意思,那 ...

  8. java web项目导入问题

    由于工作问题,接触到很多其他人的项目,有时候想要直接跑起来,还是需要一定的耐力. 1:导入不进去,干脆新建项目,一个个复制进去: 2:第三方jar包不要忘记,核实jdk    j2ee的版本,编译路径 ...

  9. Xcode5 支持 SVN 1.7

    Xcode升级了之后出现了各种问题,SVN升级到subversion 1.7后,Xcode自带有svn,版本是1.6,所以svn的1.6和1.7不兼容. 解决的办法,要么是降低系统的svn 版本,要么 ...

  10. Yii系列教程(二):功能简介

    1 MVC架构 1.1处理流程 一个Web请求在Yii内部的执行流程如下图所示: 1.2组件角色 组件名 角色与责任 index.php 入口脚本.创建Application的单例对象. applic ...