直接转化为字符串再数有几个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'); } };