题解 | #整数中1出现的次数(从1到n整数中1出现的次数)
整数中1出现的次数(从1到n整数中1出现的次数)
https://www.nowcoder.com/practice/bd7f978302044eee894445e244c7eee6
function NumberOf1Between1AndN_Solution(n)
{
// write code here
let count = 0
for (let i = 1; i <= n; i++) {
const str = String(i)
for (let j = 0; j < str.length; j ++) {
if (str[j] === '1') count++
}
}
return count
}
module.exports = {
NumberOf1Between1AndN_Solution : NumberOf1Between1AndN_Solution
};
查看12道真题和解析