Add Digits 解答
Question
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?
Hint:
- A naive implementation of the above process is trivial. Could you come up with other methods?
- What are all the possible results?
- How do they occur, periodically or randomly?
- You may find this Wikipedia article useful.
Solution
Naive way is to use two loops.
public class Solution {
public int addDigits(int num) {
int copy = num;
while (copy / 10 > 0) {
int newNum = 0;
while (copy / 10 > 0) {
newNum += copy % 10;
copy /= 10;
}
newNum += copy;
copy = newNum;
}
return copy;
}
}
There is a math relationship.
public class Solution {
public int addDigits(int num) {
if (num == 0)
return num;
if (num % 9 == 0)
return 9;
return (num % 9);
}
}
Add Digits 解答的更多相关文章
- 【LeetCode】Add Digits
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- 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 ...
- 【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] Add Digits (a New question added)
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- 258. Add Digits(C++)
258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has ...
- 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 ...
- 【02_258】Add Digits
Add Digits Total Accepted: 49702 Total Submissions: 104483 Difficulty: Easy Given a non-negative int ...
- LeetCode:Add Digits - 非负整数各位相加
1.题目名称 Add Digits (非负整数各位相加) 2.题目地址 https://leetcode.com/problems/add-digits/ 3.题目内容 英文:Given a non- ...
- LN : leetcode 258 Add Digits
lc 258 Add Digits lc 258 Add Digits Given a non-negative integer num, repeatedly add all its digits ...
随机推荐
- bzoj1379 [Baltic2001]Postman
Description 邮递员每天给N个村子的人送信,每个村子可能在某个十字路口上,或一条路的中央. 村子里的人都希望早点收到信,因此与邮递员达成一个协议:每个村子都有一个期望值Wi,如果这个村子是邮 ...
- c++ 02
一.堆内存的动态分配与释放 malloc/calloc/realloc/free new/delete:详见memory.cpp 1.通过new运算符分配单个变量 数据类型* 指针变量 = new 数 ...
- Raw qcow qcow2 vhd-vpc虚拟磁盘格式间相互转换
- javascrpit开发连连看记录-小游戏
工作之余,总想做点什么有意思的东西.但是苦于不知道做什么,也就一直没有什么动作.在一个午饭后,跟@jedmeng和@墨尘聊天过程中,发现可以写一些小东西来练练手,有以下几点好处: 1. ...
- MySQL中多表删除方法(转载)
如果您是才接触MySQL数据库的新人,那么MySQL中多表删除是您一定需要掌握的,下面就将为详细介绍MySQL中多表删除的方法,供您参考,希望对你学习掌握MySQL中多表删除能有所帮助. 1.从MyS ...
- IOS 退出App
UIApplication *app = [UIApplication sharedApplication]; [app performSelector:@selector(suspend)]; // ...
- mysql增量备份 percona-xtrabackup
先说下实际环境 阿里云买的ESC跑的mysql服务,目前由于数据量过大,数据库目前有800多GB,每次全备需要等20多个小时才能够完成,然后就想到做增量备份,写下此文档 一.测试环境 [root@lo ...
- 简单的Dao设计模式
简单的DAO设计模式 这两天学习到了DAO(Data Access Object 数据存取对象)设计模式.想谈谈自己的感受,刚开始接触是感觉有点难,觉得自己逻辑理不清,主要是以前学的知识比较零散没有很 ...
- PHP连接Mysql服务器的操作
我们的数据存储在数据库中以后,要把数据和网页联系起来的话,要通过web服务器的解释器进行读取数据,再传递给客户端网页.如图: 这里,我选择了PHP作为学习的解释器.下面就具体来总结一下PHP连接MYS ...
- Hibernate PO对象的状态
Hibernate的PO对象有三种状态:临时状态(又称临时态).持久状态(又称为持久态)和脱管状态(又称为脱管态.游离态).处理持久态的对象也称为PO,临时对象和脱管对象也称为VO. 1.临时态: 简 ...