add: 只出现一次的数字等
This commit is contained in:
15
src/math/coin-lcci.js
Normal file
15
src/math/coin-lcci.js
Normal file
@ -0,0 +1,15 @@
|
||||
/**
|
||||
* 参考:https://leetcode-cn.com/problems/coin-lcci/solution/jian-dan-de-shu-xue-jia-fa-by-zindler/
|
||||
* @param {number} n
|
||||
* @return {number}
|
||||
*/
|
||||
export const waysToChange = function (n) {
|
||||
const mod = 1e9 + 7
|
||||
let res = 0
|
||||
for (let i = 0; i <= ~~(n / 25); i++) {
|
||||
const a = ~~((n - i * 25) / 10)
|
||||
const t = (a + 1) * (~~(n / 5) - 5 * i - a + 1)
|
||||
res = (res + t) % mod
|
||||
}
|
||||
return res
|
||||
}
|
10
src/math/factorial-trailing-zeroes.js
Normal file
10
src/math/factorial-trailing-zeroes.js
Normal file
@ -0,0 +1,10 @@
|
||||
// LeetCode 172. 阶乘后的零 https://leetcode-cn.com/problems/factorial-trailing-zeroes/submissions/
|
||||
// LintCode 2. 尾部的零 https://www.lintcode.com/problem/trailing-zeros/description
|
||||
|
||||
export default (n) => {
|
||||
let sum = 0
|
||||
while (n !== 0) {
|
||||
sum += Math.floor(n /= 5)
|
||||
}
|
||||
return sum
|
||||
}
|
15
src/math/permutations.js
Normal file
15
src/math/permutations.js
Normal file
@ -0,0 +1,15 @@
|
||||
/**
|
||||
* @param {number[]} nums
|
||||
* @return {number[][]}
|
||||
*/
|
||||
export const permute = function (nums) {
|
||||
const res = []
|
||||
const backtrack = (path = []) => {
|
||||
if (path.length === nums.length) res.push(path)
|
||||
for (const n of nums) {
|
||||
!path.includes(n) && backtrack(path.concat(n))
|
||||
}
|
||||
}
|
||||
backtrack()
|
||||
return res
|
||||
}
|
38
src/math/ugly-number-ii.js
Normal file
38
src/math/ugly-number-ii.js
Normal file
@ -0,0 +1,38 @@
|
||||
/**
|
||||
* @param n: An integer
|
||||
* @return: return a integer as description.
|
||||
*/
|
||||
export const nthUglyNumber = function (n) {
|
||||
const res = [1]
|
||||
let inx2 = 0
|
||||
let inx3 = 0
|
||||
let inx5 = 0
|
||||
|
||||
for (let i = 1; i < n; i++) {
|
||||
const temp2 = res[inx2] * 2
|
||||
const temp3 = res[inx3] * 3
|
||||
const temp5 = res[inx5] * 5
|
||||
const min = Math.min(temp2, temp3, temp5)
|
||||
if (min === temp2) inx2++
|
||||
if (min === temp3) inx3++
|
||||
if (min === temp5) inx5++
|
||||
|
||||
res.push(min)
|
||||
}
|
||||
|
||||
return res[n - 1] || 0
|
||||
}
|
||||
|
||||
// 思路:
|
||||
// 一开始,丑数只有{1},1可以同2,3,5相乘,取最小的1×2=2添加到丑数序列中。
|
||||
|
||||
// 现在丑数中有{1,2},在上一步中,1已经同2相乘过了,所以今后没必要再比较1×2了,我们说1失去了同2相乘的资格。
|
||||
|
||||
// 现在1有与3,5相乘的资格,2有与2,3,5相乘的资格,但是2×3和2×5是没必要比较的,因为有比它更小的1可以同3,5相乘,所以我们只需要比较1×3,1×5,2×2。
|
||||
|
||||
// 依此类推,每次我们都分别比较有资格同2,3,5相乘的最小丑数,选择最小的那个作为下一个丑数,假设选择到的这个丑数是同i(i=2,3,5)相乘得到的,所以它失去了同i相乘的资格,把对应的pi++,让pi指向下一个丑数即可。
|
||||
|
||||
// 作者:zzxn
|
||||
// 链接:https://leetcode-cn.com/problems/ugly-number-ii/solution/san-zhi-zhen-fang-fa-de-li-jie-fang-shi-by-zzxn/
|
||||
// 来源:力扣(LeetCode)
|
||||
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
|
Reference in New Issue
Block a user