Given an integer n, return the number of trailing zeroes in n!.

Note: Your solution should be in polynomial time complexity.

题目大意:

给定一个整数n,返回n!(n的阶乘)数字中的后缀0的个数。

注意:你的解法应该满足多项式时间复杂度。

解题思路:

参考博文:http://www.geeksforgeeks.org/count-trailing-zeroes-factorial-number/

朴素解法:

首先求出n!,然后计算末尾0的个数。(重复÷10,直到余数非0)

该解法在输入的数字稍大时就会导致阶乘得数溢出,不足取。

O(logn)解法:

一个更聪明的解法是:考虑n!的质数因子。后缀0总是由质因子2和质因子5相乘得来的。如果我们可以计数2和5的个数,问题就解决了。考虑下面的例子:

n = 5: 5!的质因子中 (2 * 2 * 2 * 3 * 5)包含一个5和三个2。因而后缀0的个数是1。

n = 11: 11!的质因子中(2^8 * 3^4 * 5^2 * 7)包含两个5和三个2。于是后缀0的个数就是2。

我们很容易观察到质因子中2的个数总是大于等于5的个数。因此只要计数5的个数就可以了。那么怎样计算n!的质因子中所有5的个数呢?一个简单的方法是计算floor(n/5)。例如,7!有一个5,10!有两个5。除此之外,还有一件事情要考虑。诸如25,125之类的数字有不止一个5。例如,如果我们考虑28!,我们得到一个额外的5,并且0的总数变成了6。处理这个问题也很简单,首先对n÷5,移除所有的单个5,然后÷25,移除额外的5,以此类推。下面是归纳出的计算后缀0的公式。

By given number 4617.

5^1 : 4617 ÷ 5 = 923.4, so we get 923 factors of 5

5^2 : 4617 ÷ 25 = 184.68, so we get 184 additional factors of 5

5^3 : 4617 ÷ 125 = 36.936, so we get 36 additional factors of 5

5^4 : 4617 ÷ 625 = 7.3872, so we get 7 additional factors of 5

5^5 : 4617 ÷ 3125 = 1.47744, so we get 1 more factor of 5

5^6 : 4617 ÷ 15625 = 0.295488, which is less than 1, so stop here.

  1. public class Solution {
  2. public int trailingZeroes(int n) {//0的个数取决于n中质数5的个数,因为2的个数总是大于5,注意这里i的类型是long,不是int,否则会溢出
  3. int result = 0;
  4. for(long i=5; n/i>0; i*=5){
  5. result += (n/i);
  6. }
  7. return result;
  8. }
  9. }

算法1耗时1ms, 更简单的算法是算法2,计算单个5被除的次数,耗时2ms

  1. public class Solution {
  2. public int trailingZeroes(int n) {//0的个数取决于n中质数5的个数,
  3. int zeroCount = 0;
  4. while(n>0){
  5. n/=5;
  6. zeroCount+=n;
  7. }
  8. return zeroCount;
  9. }
  10. }

Pascal's Triangle II

Given an index k, return the kth row of the Pascal's triangle.

For example, given k = 3,
Return [1,3,3,1].

  1. public class Solution {
  2. public List<Integer> getRow(int rowIndex) {
  3. /*first method used in Pascal's Triangle, just return the wanted row
  4.  
  5. List<List<Integer>> listReturn = new ArrayList<List<Integer>>();
  6. for(int i=0;i<rowIndex+1;i++){
  7. List<Integer> tempList= new ArrayList<Integer>();
  8. for(int j=0;j<=i;j++){
  9. if(j==0||j==i)tempList.add(1);
  10. else tempList.add(listReturn.get(i-1).get(j-1)+listReturn.get(i-1).get(j));
  11. }
  12. listReturn.add(tempList);
  13. }
  14. return listReturn.get(rowIndex);*/
  15.  
  16. // second way: deal with the wanted row directly, look it as a integration of all the before rows
  17. //把第三行看成是第1、2行的结合,先构造第1、2行的data,然后按照相同的原则计算第3行:K(i)(j)=K(i-1)(j-1)+K(i-1)(j)
  18. //例如,先构造[1,1],[1,2,1],[1,3,3,1]
  19. List<Integer> ret = new ArrayList<Integer>();
  20. ret.add(1);//the first element
  21. for (int i = 1; i <= rowIndex; i++) {
  22. for (int j = i - 1; j >= 1; j--) {
  23. int tmp = ret.get(j - 1) + ret.get(j);
  24. ret.set(j, tmp);
  25. }
  26. ret.add(1);//the last element
  27. }
  28. return ret;
  29.  
  30. }
  31. }
  1.  

数字规律:Pascal‘s triangle的更多相关文章

  1. ZOJ-Little Sub and Pascal's Triangle(思维规律)

    Little Sub is about to take a math exam at school. As he is very confident, he believes there is no ...

  2. 【leetcode】Pascal's Triangle I & II (middle)

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  3. 【Leetcode】【Easy】Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  4. LeetCode(118) Pascal's Triangle

    题目 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, R ...

  5. [LeetCode] Pascal's Triangle II 杨辉三角之二

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  6. [LeetCode] Pascal's Triangle 杨辉三角

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  7. 118. Pascal's Triangle

    题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...

  8. [LeetCode]题解(python):118 Pascal's Triangle

    题目来源 https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numRows of Pa ...

  9. [LeetCode 118] - 杨辉三角形(Pascal's Triangle)

    问题 给出变量numRows,生成杨辉三角形的前numRows行. 例如,给出numRows=5,返回: [     [1],    [1,1],   [1,2,1],  [1,3,3,1], [1, ...

随机推荐

  1. Json.net对数据的解析

    在官网下载Json.net文件后,解压完将Net20下面的DLL复制到Assets目录下. using UnityEngine; using System.Collections; using New ...

  2. Mysql中int(2)和int(10)的区别

    int(N)中的N不是限制字段取值范围的,int的取值范围是固定的(0至4294967295)或(-2147483648至2147483647) 那么N这个值是为了在字段中的值不够时补零的,但是必须含 ...

  3. 关于表单提交submit的兼容性问题。

    这里的form 表单 点击下载执行的函数名字是submit,这样不规范,submit是提交表单,函数名字不能取名叫submit,如果取名叫submit会在低版本的浏览器上无法识别,导致直接提交表单,无 ...

  4. 安卓布局修改基础常识篇之TextView属性

    [天使]安卓布局修改基础常识篇之TextView属性 在修改布局xml文件时需要熟练掌握一些属性,以下是TextView也就是文本的属性:android:autoLink 是否自动链接网址或邮箱地址: ...

  5. ubuntu设置字体编码GBK和UTF-8

    http://www.xwuxin.com/?p=1155 http://zhidao.baidu.com/link?url=T6m7WDVOM1VBiUctkfkf1qO14lSMLhxg6MIRt ...

  6. Python 之路:Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy

    一.Memcached Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负债.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速 ...

  7. C语言数据类型的表示范围

    1.C和C++语言中基本的数据类型有:字符型(char),整形(short, int, long), 浮点型(float, double)    类型 字节数 类型 字节数 char 1 short ...

  8. Unity3d之剥离alpha通道

    unity中, 将图集的 alpha 通道剥离出来可减少包体大小和内存使用大小. 方法是将原来的一张 rgba 图分成一张 rgb 和一张 alpha 图,android上rgb和alpha图均采用e ...

  9. 使用myeclipse新建和删除web项目时一定要小心

    今天使用myeclipse在非工作空间的路径下新建一个web项目,路径内包含了原有一些web项目,可是我在指定位置时,在那个路径下多写一个子目录的路径,以为myeclipse会 为我默认新建子目录以便 ...

  10. spring3mvc与struts2比较

    目前企业中使用SpringMvc的比例已经远远超过Struts2,那么两者到底有什么区别,是很多初学者比较关注的问题,下面我们就来对SpringMvc和Struts2进行各方面的比较: 1. 核 心控 ...