LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number
数学题
172. Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity. (Easy)
分析:求n的阶乘中末位0的个数,也就是求n!中因数5的个数(2比5多),简单思路是遍历一遍,对于每个数,以此除以5求其因数5的个数,但会超时。
考虑到一个数n比他小能被5整除的数的个数是一定的(n / 5),由此再考虑能被25整除,125整除的数的个数,得到如下算法:
代码:
class Solution {
public:
int trailingZeroes(int n) {
int sum = ;
while (n > ) {
sum += (n / );
n /= ;
}
return sum;
}
};
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. (Easy)
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
分析:
考虑到
ab % 9 = (9a + a + b) % 9 = (a + b) % 9;
abc % 9 = (99a + 9 b + a + b + c) % 9 = (a + b + c) % 9;
所以求到其只有个位数位置即用其mod 9即可,考虑到被9整除的数应该返回9而非0,采用先减一再加一方式处理。
代码:
class Solution {
public:
int addDigits(int num) {
if (num == ) {
return ;
}
return (num - ) % + ;
}
};
268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n
, find the one that is missing from the array.
For example,
Given nums = [0, 1, 3]
return 2
. (Medium)
分析:
采用先求和(前n项和),再将求和结果与数组和相减的方法,求得差哪个数
代码:
class Solution {
public:
int missingNumber(vector<int>& nums) {
int n = nums.size();
int sum1 = n * (n + ) / ;
int sum2 = ;
for (int i = ; i < nums.size(); ++i) {
sum2 += nums[i];
}
return sum1 - sum2;
}
};
LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number的更多相关文章
- 每天一道LeetCode--172. Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- LeetCode----172. Factorial Trailing Zeroes(Java)
package singlenumber136; //Given an array of integers, every element appears twice except for one. F ...
- LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)
172. 阶乘后的零 172. Factorial Trailing Zeroes 题目描述 给定一个整数 n,返回 n! 结果尾数中零的数量. LeetCode172. Factorial Trai ...
- 【LeetCode】172. Factorial Trailing Zeroes
Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your ...
- LeetCode Day4——Factorial Trailing Zeroes
/* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...
- LeetCode Factorial Trailing Zeroes Python
Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘 ...
- LeetCode_172. Factorial Trailing Zeroes
172. Factorial Trailing Zeroes Easy Given an integer n, return the number of trailing zeroes in n!. ...
- [Swift]LeetCode172. 阶乘后的零 | Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...
- [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
随机推荐
- Activiti实战03_Hello World
Hello World如此经典,以至于几乎学习没一门新的技术都是从Hello World开始,可能意味着开启了新世界的大门吧,接下来就让我们一起步入到Activiti的世界中吧! 本文所使用开发环境 ...
- 《DSP using MATLAB》Problem 7.33
代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...
- 如何让 J2Cache 在多种编程语言环境中使用
现在的系统是越来越复杂了,不仅仅是功能复杂,系统结构也非常复杂,而且经常在一个系统里包含几种不同语言编写的子系统.例如用 JavaScript 做前端开发.用 Java/PHP 等等做后端,C/C++ ...
- TZOJ 4292 Count the Trees(树hash)
描述 A binary tree is a tree data structure in which each node has at most two child nodes, usually di ...
- stringstream的使用 UVA 10815
水题题目描述就不写了 主要是发现stringstream真的是好用,可以把string绑定到stringstream中,然后就能以空格为分隔符分割出每个单词,听说每次重新创建stringstream开 ...
- JS---案例:高清放大镜
案例:高清放大镜 分3步 1. 鼠标进入和离开,显示和隐藏遮挡层和大图div 2. 鼠标在小层上移动,鼠标横纵坐标,为可视区域坐标-遮挡层的宽高,鼠标移动的时候,在一个区域内移动,需要判断和定义下移动 ...
- htmlunit学习之java.lang.NoSuchMethodError: com.gargoylesoftware.htmlunit.WebClient.getOptions()Lcom/gargoylesoftware/htmlunit/WebClientOptions;
运行到这里就报错 java.lang.NoSuchMethodError: com.gargoylesoftware.htmlunit.WebClient.getOptions()Lcom/gargo ...
- ifconfig命令为centos linux系统配置临时的局域名IP、网关以及子网掩码
ifconfig eth0 192.168.1.25 netmask 255.255.255.0 broadcast 192.168.1.1 up netmask:子网掩码broadcast:默认网关
- top进程命令
top命令用来显示系统当前的进程状况. 格式:top [选项] 主要选项如下. d:指定更新的间隔,以秒计算. q:没有任何延迟的更新.如果使用者有超级用户,则top命令将会以最高的优先序执行. c: ...
- 一个基于swoole的作业调度组件,已经实现了redis和rabitmq队列消息存储。
https://github.com/kcloze/swoole-jobs 一个基于swoole的作业调度组件,已经实现了redis和rabitmq队列消息存储.参考资料:swoole https:/ ...