题解 | 整数中1出现的次数(从1到n整数中1出现的次数)

整数中1出现的次数(从1到n整数中1出现的次数)

https://www.nowcoder.com/practice/bd7f978302044eee894445e244c7eee6

#include <string>
#include <iostream>
class Solution {
public:
    int NumberOf1Between1AndN_Solution(int n) {
        int count = 0;
        for(int i = 1; i <= n; i++){
            string number = to_string(i);
            count += std::count(number.begin(),number.end(),'1');
        }
        return count;
    
    }
};

我的思路简单粗暴,将每一个数字转成字符串,然后统计这个字符串中‘1’的数量。

全部评论

相关推荐

下个早班:秒挂就是不缺人
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务