Skip to content

Commit

Permalink
update 233
Browse files Browse the repository at this point in the history
  • Loading branch information
selfboot committed Apr 28, 2016
1 parent c27abb3 commit d4a1e82
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
37 changes: 37 additions & 0 deletions Math/233_NumberOfDigitOne.cpp
@@ -0,0 +1,37 @@
/*
* @Author: xuezaigds@gmail.com
* @Last Modified time: 2016-04-28 13:58:30
*/

class Solution {
public:
int countDigitOne(int n)
{
if(n<=0) return 0;
long long count = 1;
while(n){
if(n<10){
break;
}
int digit = n % 10;
n /= 10;
count += n;
if(digit == 0) count -= 1;
count += countDigitOne(n-1) * 10;

// 最后一行中数组1出现的次数
while(n){
if(n%10==1) count += digit+1;
n /= 10;
}
}
return count;
}
};

/*
-1
6
12
234545
*/
2 changes: 1 addition & 1 deletion Math/233_NumberOfDigitOne.py
Expand Up @@ -13,7 +13,7 @@ def countDigitOne(self, n):
else:
units = n % 10
tens = n / 10
count = self.countDigitOne(tens-1) * 10 + tens
count = self.countDigitOne(tens - 1) * 10 + tens
n /= 10
while n:
if n % 10 == 1:
Expand Down

0 comments on commit d4a1e82

Please sign in to comment.