题解 | #牛群全排列数#
牛群全排列数
https://www.nowcoder.com/practice/5ab233c23fcc4c69b81bd5a66c07041c
知识点
递归
解题思路
递归的思路为当前n的总数为上一个总数*n,也就是 fun(n) = fun(n-1)*n;
需要考虑当fun(n-1)*n超出int范围,所以需要先把他们转成long之后取模转成int。
Java题解
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param n int整型
* @return int整型
*/
public int factorial (int n) {
// write code here
if(n == 1) return 1;
return (int)((long)factorial(n - 1) * n % 1000000007);
}
}
