258. Add Digits
题目:
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.
链接: http://leetcode.com/problems/add-digits/
题解:
又是数学题,求digital root。循环叠加比较容易,但看了wiki以后发现了公式,还是用公式算吧。这种数学题对数学不好的我来说真是头大。原理10 % 9 或者 100 % 9都等于 1 % 9。举个例子n = abc = a * 100 + b * 10 + c,那么 (a*100 + b * 10 + c) % 9 = (a + b + c) % 9。由此n == 0时,result = 0, n % 9 == 0时, 说明a + b + c = 9,我们返回9,对于其他数字, (a + b + c)等于res % 9。
Time Complexity - O(1), Space Complexity - O(1)
public class Solution {
public int addDigits(int num) {
return 1 + (num - 1) % 9;
}
}
二刷:
Java:
public class Solution {
public int addDigits(int num) {
return 1 + (num - 1) % 9;
}
}
三刷:
发现前两刷其实并没有完全理解,也许就是看了discuss区的答案而已。为什么(a + b + c) mod 9 = (abc) mod 9, 真正用到的公式是modulo运算的分配和结合律。
1. (a + b) mod n = ((a mod n) + (b mod n)) mod n
2. (a * b) mod n = ((a mod n) * (b mod n)) mod n
假如一个数字的三位字符是abc,那么这个数等于 a * 100 + b * 10 + c, 根据分配律, (a * 100) mod 9 = ((a mod 9) * (100 mod 9)) mod 9 = a mod 9,b和c同理, 所以 (a * 100 + b * 10 + c) mod 9 = (a + b + c) mod 9。 我们还可以使用一个小技巧,再用一次分配律直接用 (num - 1) mod 9 + 1来得到结果,这样可以避免一些边界条件的判断。
public class Solution {
public int addDigits(int num) {
if (num == 0) {
return 0;
}
int res = num % 9;
return res == 0 ? 9 : res;
}
}
public class Solution {
public int addDigits(int num) {
return 1 + (num - 1) % 9;
}
}
Update:
public class Solution {
public int addDigits(int num) {
if (num <= 0) return 0;
return (num % 9 == 0) ? 9 : num % 9;
}
}
Reference:
https://en.wikipedia.org/wiki/Digital_root
https://en.wikipedia.org/wiki/Modulo_operation
https://leetcode.com/discuss/67755/3-methods-for-python-with-explains
https://leetcode.com/discuss/52122/accepted-time-space-line-solution-with-detail-explanations
https://leetcode.com/discuss/55910/two-lines-c-code-with-explanation
258. Add Digits的更多相关文章
- 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 ...
- LN : leetcode 258 Add Digits
lc 258 Add Digits lc 258 Add Digits Given a non-negative integer num, repeatedly add all its digits ...
- 【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 258. Add Digits
Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...
- (easy)LeetCode 258.Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- Java [Leetcode 258]Add Digits
题目描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...
- 【LeetCode】258. Add Digits
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
- 【一天一道LeetCode】#258. Add Digits
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- TCP连接,传输数据时的粘包问题讨论
第一个需要讨论的大概就是粘包问题了.因为这个是TCP的个性问题,UDP通信时不存在这个问题的.首先看一下什么叫粘包: 客户端采取与服务器的长连接方式建立通信(Open-Write/Read-Write ...
- 浅谈 WPF布局
我们首先来了解一下图形化用户界面(Graphic User Interface)也就是我们常常听到的GUI.举个简单的例子,同样是数据,我们可以用控制台程序加格式控制符等输出,但是这些都不如GUI来的 ...
- STM32普通定时器实现延时函数
/* SystemFrequency / 1000 1ms中断一次 * SystemFrequency / 100000 10us中断一次 * SystemFrequency / 1000000 1u ...
- ServiceStack.OrmLite 调用存储过程
最近在做关于ServiceStack.OrmLite调用存储过程时,有问题.发现ServiceStack.OrmLite不能调用存储过程,或者说不能实现我想要的需求.在做分页查询时,我需要传入参数传出 ...
- 深度分析 Java 的 ClassLoader 机制(源码级别)
写在前面:Java中的所有类,必须被装载到jvm中才能运行,这个装载工作是由jvm中的类装载器完成的,类装载器所做的工作实质是把类文件从硬盘读取到内存中,JVM在加载类的时候,都是通过ClassLoa ...
- android 有时候stroke不起作用
如下: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android=&quo ...
- socket编程实现HTTP请求
利用c++语言+socket实现HTTP请求,请求获得的数据效果图如下: HTTP协议的下一层是TCP,根据HTTP协议只需要利用TCP发送下面的数据到达目标主机,目标主机就会发送相应的数据到客户端. ...
- tomcat内存溢出,设置
tomcat/bin/catalina.bat里找到echo Using CATALINA_BASE: "%CATALINA_BASE%" ,在上方设置: set JAV ...
- MySQL 体系架构
MySQL 体系架构 本篇文章是对mysql体系结构进行了详细的分析介绍,需要的朋友参考下 上面一图是mysql的概览图,我们从上往下看, 我们把上面一图一分为二,我们可以吧它分为两个部分, 1,是c ...
- 编写一个小程序,从标准输入读入一系列string对象,寻找连续重复出现的单词。程序应该找出满足一下条件的单词:该单词的后面紧接着再次出现自己本身。跟踪重复次数最多的单词及其重复次数,输出.
// test13.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...