数学题

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

代码:

 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的更多相关文章

  1. 每天一道LeetCode--172. Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

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

    package singlenumber136; //Given an array of integers, every element appears twice except for one. F ...

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

    172. 阶乘后的零 172. Factorial Trailing Zeroes 题目描述 给定一个整数 n,返回 n! 结果尾数中零的数量. LeetCode172. Factorial Trai ...

  4. 【LeetCode】172. Factorial Trailing Zeroes

    Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your ...

  5. LeetCode Day4——Factorial Trailing Zeroes

    /* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...

  6. LeetCode Factorial Trailing Zeroes Python

    Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘 ...

  7. LeetCode_172. Factorial Trailing Zeroes

    172. Factorial Trailing Zeroes Easy Given an integer n, return the number of trailing zeroes in n!. ...

  8. [Swift]LeetCode172. 阶乘后的零 | Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...

  9. [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

随机推荐

  1. 微信小程序开发(一)

    引自http://www.cnblogs.com/mdnx/p/6004653.html 第一步  (下载开发工具) https://mp.weixin.qq.com/debug/wxadoc/dev ...

  2. NOSQL数据库之 REDIS

    NOSQL数据库之 REDIS 一.NOSQL 1.简介 NoSQL ,(Not Only SQL),泛指非关系型数据库. 特点: NoSQL 通常是以key-value形式存储, 不支持SQL语句, ...

  3. alert对象相关问题

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. light oj 1149 Factors and Multiples(二分匹配)

    LightOJ1149 :Factors and Multiples 时间限制:2000MS    内存限制:32768KByte   64位IO格式:%lld & %llu 描述 You w ...

  5. cookie记录

    登录页面引用: <script src="/jquery.cookie.js"></script> 登录页面jq: var telphone = $('[n ...

  6. 压缩过的jQuery

    /*! jQuery v3.4.1 | (c) JS Foundation and other contributors | jquery.org/license */ !function(e,t){ ...

  7. angular可以做的小功能 未完成

    1,网上购物满多少减多少 思路: 效果图,满500减10元邮费 1,html部分有基本布局, <div> <h3>化妆品</h3> 单价 <input typ ...

  8. js经典校验之注册与登录校验

    平时都专注于后台功能的实现和逻辑需求的分析及数据库方面的设计,很少关注前端的设计,而项目开发过程中专门负责后台是不太可能的事,所以前端我们也需要会用,除了漂亮的首页等其他的交给美工来做,一些功能性的东 ...

  9. 2019阿里云开年Hi购季新用户分会场全攻略!

    2019阿里云云上Hi购季活动已经于2月25日正式开启,从已开放的活动页面来看,活动分为三个阶段: 2月25日-3月04日的活动报名阶段.3月04日-3月16日的新购满返+5折抢购阶段.3月16日-3 ...

  10. Laravel 中 offset,limit 或 skip , take 的使用

    laravel 本身有一个自带的快速分页方法 paginate,只需要传入每页显示多少条数据就可以 了,但是如果想使用自定义从哪里开始呢. 一.offset,limit (offset 设置从哪里开始 ...