题解 | #整数中1出现的次数(从1到n整数中1出现的次数)#
整数中1出现的次数(从1到n整数中1出现的次数)
http://www.nowcoder.com/practice/bd7f978302044eee894445e244c7eee6
public class Solution { public int NumberOf1Between1AndN_Solution(int n) { int wei = 1 ;//个十百 int count = 0 ; while(n / wei != 0) { int high = n/wei/10 ; int cur = n/wei%10 ; int low = n%wei; if(cur == 0) { count += high * wei ; } else if(cur == 1) { count += ((low + 1) + high*wei) ; } else { count += (high + 1) * wei ; } wei *= 10 ; } return count ; } }
一个菜鸟的算法刷题记录 文章被收录于专栏
分享一个菜鸟的成长记录