Description:

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?

题意很简单,最容易想到的办法就是循环迭代,如果是<10的就返回。下面是一种递归的方法。

public class Solution {
public int addDigits(int num) {
if(num < 10) {
return num;
}
//1023
int t = 0;
while(num >= 1) {
t += num % 10;
num = num / 10;
} return addDigits(t);
}
}

  那么怎么做才能不用递归和循环迭代来把复杂度降到O(1)呢,这让我联想到了公式。来通过数据找找规律。

前30个数据测试:

public static void main(String[] args) {
for(int i=0; i<30; i++) {
System.out.println(i + " " + addDigits(i));
}
}

  

0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 1
11 2
12 3
13 4
14 5
15 6
16 7
17 8
18 9
19 1
20 2
21 3
22 4
23 5
24 6
25 7
26 8
27 9
28 1
29 2

找出来规律了吧。

其实是这样的:(num-1) % 9 + 1

public class Solution {
public int addDigits(int num) {
return (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

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

  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. kernel 生成uImage

    有些u-boot 指定要链接uImage 所以今天记录一下kernel 生成uImage 的条件 在kernel 配置完make menuconfig 后,进行编译,发现make uImage 生成不 ...

  2. [Makefile]多目录Makefile写法

    最近需要写一个测试程序,这个测试程序需要集成一些功能,写在同一个文件看上去很不好,多个文件的Makefile又不是很熟,于是分享下面这篇文章 先介绍下,调试Makefile时,想知道某个变量的值,怎么 ...

  3. lua——string之string.gsub

    translated from the lua document string.gsub用法: 函数原型:string.gsub( s, pattern, rep1[, n] ) 函数功能:返回一个和 ...

  4. iOS彩票项目--第三天,搭建竞技场和发现,搭建幸运选号和我的彩票界面

    一.竞技场搭建--UISegmentedControl的使用 // 重写 自定义控制器的view - (void)loadView { UIImageView *imgView = [[UIImage ...

  5. WPF教程六:布局之Grid面板

    Grid:网格面板 Grid顾名思义就是“网格”,以表格形式布局元素,对于整个面板上的元素进行布局,它的子控件被放在一个一个事先定义好的小格子里面,整齐配列. Grid和其他各个Panel比较起来,功 ...

  6. 隐藏的Swiper显示后无法获取正确的宽度和高度

    今天在使用swiper的时候,元素默认是显示的时候没毛病,但是默认是隐藏的状态,再显示的时候发现滑动的时候宽度计算有误,如下图所示: 正确的显示如下: 隐藏的元素再次显示如下: 宽度计算有误 解决方案 ...

  7. 关于Cocos2d-x中根据分数增加游戏难度的方法

    1.GameScene.h中声明一些分数边界值 //level提升所需的分数 enum LevelUp_Score { Level1Up_Score = , Level2Up_Score = , Le ...

  8. 严格控制GOTO语句

    注意事项 1,使用顺序.选择.循环等有限的基本结构表示程序逻辑. 2,选用的控制结构只准许有一个入口和一个出口 3,程序语句组成容易识别的块,每块只有一个入口和一个出口. 4,复杂结构应该用基本控制结 ...

  9. php -- php控制linux关机、重启、注销

        php 里面有个 system(exec) 方法, 可以调用系统命令.     重启先建立一个脚本(比喻 /root/reboot_server.sh ),重启用的.//路径可随便,但最少必须 ...

  10. asp.net mvc webconfig配置文件操作

    读取web.config数据,可以不用编译.如发布后,非常有用web.config文件<configuration> <appSettings> <add key=&qu ...