题解 | #填充数组#
填充数组
https://www.nowcoder.com/practice/3e34d9ed0bbc4db68b6fbca20a62926d
其实就是简单结合排列组合和大数计算
package main import "math/big" /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param a int整型一维数组 * @param k int整型 * @return int整型 */ func FillArray(a []int, k int) int { // write code here startNum, count := 1, 0 res := int64(1) for i := 0; i < len(a); i++ { if a[i] == 0 { count++ continue } if count != 0 { res = (res * getSection(a[i]-startNum, count)) % 1000000007 } startNum = a[i] count = 0 } if count != 0 { res = (res * getSection(k-startNum, count)) % 1000000007 } return int(res) } func getSection(inter, num int) int64 { if num == 0 || inter < 0 { return 0 } if inter == 0 { return 1 } res := big.NewInt(1) for i := num + inter; i > num; i-- { res.Mul(res, big.NewInt(int64(i))) } for i := 1; i <= inter; i++ { res.Div(res, big.NewInt(int64(i))) } return res.Mod(res, big.NewInt(1000000007)).Int64() }