题解 | #整数中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
};

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务