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整除的数的个数,得到如下算法:

代码:

 1 class Solution {
 2 public:
 3     int trailingZeroes(int n) {
 4         int sum = 0;
 5         while (n > 0) {
 6             sum += (n / 5);
 7             n /= 5;
 8         }
 9         return sum;
10     }
11 };

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 = 111 + 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,采用先减一再加一方式处理。

代码:

1 class Solution {
2 public:
3     int addDigits(int num) {
4         if (num == 0) {
5             return 0;
6         }
7         return (num - 1) % 9 + 1;
8     }
9 };

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项和),再将求和结果与数组和相减的方法,求得差哪个数

代码:

 1 class Solution {
 2 public:
 3     int missingNumber(vector<int>& nums) {
 4         int n = nums.size();
 5         int sum1 = n * (n + 1) / 2;
 6         int sum2 = 0;
 7         for (int i = 0; i < nums.size(); ++i) {
 8             sum2 += nums[i];
 9         }
10         return sum1 - sum2;
11     }
12 };
时间: 2024-10-05 13:28:40

LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number的相关文章

LeetCode172——Factorial Trailing Zeroes

Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 难度系数:容易 题目大意:给定一个整数n,返回n!的末尾0的个数,要求对数阶时间复杂度. 乍一看,这确实不难,很简单的做法是先求n!嘛,然后求0的个数.结果是没问题,但是时间复杂度不符合要求,求n!就要O(n)的时间复杂度. 虽然说那样做不符合要求

每天一道LeetCode--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. 此题是求阶乘后面零的个数. public class Solution { public int trailingZeroes(int n) { int t=0; while(n!=0){ n/=5; t+=n; } return t; } }

[Leetcode172]Factorial Trailing Zeroes

Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. solution: zero comes from 2*5, and number of 2 is less than 5. So we can only count the number of 5 contained in n!. public

LeetCode----172. Factorial Trailing Zeroes(Java)

1 package singlenumber136; 2 //Given an array of integers, every element appears twice except for one. Find that single one. 3 //Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 4 public class

LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)

172. 阶乘后的零 LeetCode172. Factorial Trailing Zeroes 题目描述 给定一个整数 n,返回 n! 结果尾数中零的数量. 示例 1: 输入: 3 输出: 0 解释: 3! = 6, 尾数中没有零. 示例 2: 输入: 5 输出: 1 解释: 5! = 120, 尾数中有 1 个零. 说明: 你算法的时间复杂度应为 O(log n) . Java 实现 class Solution { // 递归思路 public static int trailingZe

[LeetCode]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. 分析 朴素解法: 首先求出n!,然后计算末尾0的个数.(重复÷10,直到余数非0) 该解法在输入的数字稍大时就会导致阶乘得数溢出,不足取. O(logn)解法: 考虑n!的质数因子. 后缀0总是由质因子2和质因子5相乘得来的.如果我们可以计数

Factorial Trailing Zeroes

Factorial Trailing Zeroes Total Accepted: 44612 Total Submissions: 144778 Difficulty: Easy Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. (M) Number of Digit One n!的结果中0的个

LeetCode Day4——Factorial Trailing Zeroes

1 /* 2 * Problem 172: Factorial Trailing Zeroes 3 * Given an integer n, return the number of trailing zeroes in n!. 4 * Note: Your solution should be in logarithmic time complexity. 5 */ 6 7 /* 8 * Solution 1 9 * 对于每一个数字,累计计算因子10.5.2数字出现的个数,结果等于10出现的

【LeetCode】Factorial Trailing Zeroes (2 solutions)

Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. Credits:Special thanks to @ts for adding this problem and creating all test cases. 对n!做质因数分解n!=2x*