function searchone(shuzi,target){
let sum=0;
let str=shuzi.toString();
for(key of str){
if(key==target){
sum++
}
}
return sum
}
var countBits = function(n) {
var arr=new Array(n+1)
arr[0]=0
for(let i=1;i<arr.length;i++){
arr[i]=searchone(i.toString(2),1)
}
return arr
}
不同路径
javascript复制代码
var uniquePaths = function(m, n) {
const f = new Array(m).fill(0).map(() => new Array(n).fill(0));
for (let i = 0; i < m; i++) {
f[i][0] = 1;
}
for (let j = 0; j < n; j++) {
f[0][j] = 1;
}
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
f[i][j] = f[i - 1][j] + f[i][j - 1];
}
}
return f[m - 1][n - 1];
};