LeetCode——Add Digits
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 = 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?
题意很简单,最容易想到的办法就是循环迭代,如果是<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的更多相关文章
- [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. ...
- [LeetCode] Add Digits 加数字
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- (leetcode)Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- LeetCode Add Digits (规律题)
题意: 将一个整数num变成它的所有十进制位的和,重复操作,直到num的位数为1,返回num. 思路: 注意到答案的范围是在区间[0,9]的自然数,而仅当num=0才可能答案为0. 规律在于随着所给自 ...
- LeetCode:Add Digits - 非负整数各位相加
1.题目名称 Add Digits (非负整数各位相加) 2.题目地址 https://leetcode.com/problems/add-digits/ 3.题目内容 英文:Given a non- ...
- LeetCode 258. 各位相加(Add Digits)
258. 各位相加 258. Add Digits 题目描述 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. LeetCode258. Add Digits 示例: 输入: 3 ...
- 【LeetCode】Add Digits
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- 【LeetCode】258. Add Digits (2 solutions)
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- 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 ...
随机推荐
- Ubuntu下安装Apache
Ubuntu为我们提供了 su apt-get install 命令,通过它你可以很方便地安装一些软件,这些软件是放在Ubuntu放置在各个地方的服务器上面,如果你想安装的软件是比较常见的,一般都可以 ...
- java——关于数组的定义 和 访问修饰符的修饰内容
public class Shuzu { public static void main(String[] args) { // 定义数组 必须初始化长度,没有初始化要放数据 int[] in = { ...
- Idea配置sbt(window环境)
近开发spark项目使用到scala语言,这里介绍如何在idea上使用sbt来编译项目. 开发环境:windows 1. 下载sbt http://www.scala-sbt.org/download ...
- springMVC下的javascript调试
最近想弄一个hadoop的管理界面,所以在网上下了一个名为jeecg的快速开发平台,由于工作之后没有用过java做网站,遇到了好多小问题,其中一个就是现在要说的javascript脚本调试的问题.说来 ...
- Java中上传文件和表单数据提交如何保持数据的一致性?
学生申请学科竞赛活动,表单中有学科竞赛的申报信息和部分附件,需要做到将上传文件和表单数据提交保持一致性. 将上传文件和插入表单数据放到事务汇总去处理,由于表单的数据我们可以控制,但是上传的文档不好控制 ...
- 分布式下的session处理方式
分布式下的session处理方式 现在的企业级别开发下,分布式的问题是随处可见.今天我们来看看分布式情况下session的处理. 目前的处理方式有以下几种: 1.session黏性.就是说,用户在访问 ...
- Stay hungry, stay foolish. 求知若饥,虚心若愚。
如果留意我博客的朋友,应该都有看到这句话在我的自我介绍栏目存在了好长一段时间了,另外,我的 QQ.旺旺.MSN等都有这个签名.Stay hungry, stay foolish.(求知若饥,虚心若愚) ...
- 重点:怎样正确的使用QThread类(注:包括推荐使用QThread线程的新方法QObject::moveToThread)
背景描述: 以前,继承 QThread 重新实现 run() 函数是使用 QThread唯一推荐的使用方法.这是相当直观和易于使用的.但是在工作线程中使用槽机制和Qt事件循环时,一些用户使用错了.Qt ...
- PyQT中多重继承,其中继承的父类有QObject或QObject的子孙类
如果Child多重继承(Parent_1,Parent_2,Parent_3),其super函数 super(Child, self).__init__() 则会执行继承的最左侧的父类:Parent_ ...
- CentOS7.2内核版本查看简述
1.uname 命令 [root@bogon /]# uname --help 用法:uname [选项]... 输出一组系统信息.如果不跟随选项,则视为只附加-s 选项. -a, --all以如 ...