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?

 class Solution {
public:
int addDigits(int num) {
int ans = num;
// while(ans!=2)
// {
while()
{
if(ans < ) return ans;
num = ans;
ans = ;
while(num != )
{
ans += num%;
num /= ;
}
}
}
};

O(1)方法

观察如下输入:

输入:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20

输出:1,2,3,4,5,6,7,8,9,1 , 2 , 3 , 4 , 5 , 6, 7 , 8 , 9 , 1 , 2

可以看到返回的值就是(num-1)%9+1

(leetcode)Add Digits的更多相关文章

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

  2. [LeetCode] Add Digits 加数字

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

  3. LeetCode——Add Digits

    Description: Given a non-negative integer num, repeatedly add all its digits until the result has on ...

  4. LeetCode Add Digits (规律题)

    题意: 将一个整数num变成它的所有十进制位的和,重复操作,直到num的位数为1,返回num. 思路: 注意到答案的范围是在区间[0,9]的自然数,而仅当num=0才可能答案为0. 规律在于随着所给自 ...

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

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

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

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

  7. 【LeetCode】Add Digits

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

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

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

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

随机推荐

  1. lua 循环

    x= while i<x do print(x) i=i+ end then local x x = ) else print(x) end print(x)

  2. 更快、更强——解析Hadoop新一代MapReduce框架Yarn(CSDN)

    摘要:本文介绍了Hadoop 自0.23.0版本后新的MapReduce框架(Yarn)原理.优势.运作机制和配置方法等:着重介绍新的Yarn框架相对于原框架的差异及改进. 编者按:对于业界的大数据存 ...

  3. input:focus

    input:focus,select:focus,textarea:focus{outline:-webkit-focus-ring-color auto 5px;outline-offset:-2p ...

  4. OI优化开关

    #pragma comment(linker,"/STACK:10240000,10240000")#pragma GCC optimize ("O2")

  5. C#获取机器码(转)

    /// <summary>    /// 机器码    /// </summary>   public class MachineCode    {        ///   ...

  6. 使用 CJSON 在C语言中进行 JSON 的创建和解析的实例讲解

    本文用代码简单介绍cjson的使用方法,1)创建json,从json中获取数据.2)创建json数组和解析json数组 1. 创建json,从json中获取数据 #include <stdio. ...

  7. easyui datagrid 列显示和隐藏

    //当查询IT基础设施的时候隐藏'STAFF_ID'.'ITSM_STAFF_ID' if($("input[name='currentstate']").val()==2){ $ ...

  8. style在进行图形绘制前,要设置好绘图的样式

    是html5出现的新标签,像所有的dom对象一样它有自己本身的属性.方法和事件,其中就有绘图的方法,js能够调用它来进行绘图 ,最近在研读<html5与css3权威指南>下面对其中最好玩的 ...

  9. HTML练习----注册界面

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. 通过SEP屏蔽共享文件夹

    Go to Policies – Application and Device Control. Select default Application and Device Control polic ...