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 ...
随机推荐
- pip安装requests报错unicodeEncodeError:'ascii' codec can\t encode charactesers in position 9-12:ordinal not in range(128)
前提 : 已经安装pip(pip的安装我参考的是本博客转载脚本之家的步骤,实验可以成功) 1. 在cmd输入命令转到pip安装目录: 2. 运行后出现错误 3. 步骤2中的错误应该和编码有关.搜索百度 ...
- 专访阿里云资深技术专家黄省江:中国SaaS公司的成功之路
笔者采访中国SaaS厂商10多年,深感面对获客成本巨大.产品技术与功能成熟度不足.项目经营模式难以大规模复制.客户观念有待转变等诸多挑战,很多中国SaaS公司的经营状况都不容乐观. 7月26日,阿里云 ...
- agc033
A题意:给你个黑白矩阵,每次黑的周围一圈会变黑,求多少次之后全黑.n <= 1000 解:BFS即可. #include <bits/stdc++.h> ; , , -, }; , ...
- Spring相关技术记录
@ResponseStatus(value=) Hibernate OneToOne: 使用optional=false,才能使用lazy 如果关联字段是当前表的主键,我试了不加optional=fa ...
- localStorage对象简单应用 - - 访问次数
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 备忘 ubuntu ip 及 dns 的坑
以前都用 ubuntu 16.04 现在用 18.04 遇到几个恶心的事,现在解决了,记录下来. 1. 设置 DNS , DNS 设置老是不对,最后发现问题老版本 ubuntu 17.10以下 ...
- python 数据组合
- Leetcode33.Search in Rotated Sorted Array搜索旋转排序数组
假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 搜索一个给定的目标值,如果数组中存在这个目标值, ...
- 洛谷 P1892 [BOI2003]团伙
题目描述 1920年的芝加哥,出现了一群强盗.如果两个强盗遇上了,那么他们要么是朋友,要么是敌人.而且有一点是肯定的,就是: 我朋友的朋友是我的朋友: 我敌人的敌人也是我的朋友. 两个强盗是同一团伙的 ...
- Javascript面向对象编程(四):非构造函数的继承
什么叫非构造函数的继承? 比如,现在有一个对象,叫做"中国人". var Chinese = { nation:'中国' }; 还有一个对象,叫做"医生". v ...