字符串解法

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

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

直接转化为字符串再数有几个1即可

class Solution
{
public:
    int NumberOf1Between1AndN_Solution(int n)
    {
        string s;
        for(int i = 1; i <= n ;i++)
        {
            string tmp = to_string(i);
            s += tmp;
        }
        return count(s.begin(), s.end(), '1');
    }
};
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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