LintCode笔记(7)——统计数字
来源:互联网 发布:成都市行知小学怎么样 编辑:程序博客网 时间:2024/05/19 05:37
计算数字k在0到n中的出现的次数,k可能是0~9的一个值
样例
费了很大的力气找比较简单的方法,最后还是没有实现,只能用暴力破解。。思路就是找出0~N中每个数字的每一位,与k进行比较,若相等则加1,代码如下:例如n=12,k=1,在 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],我们发现1出现了5次 (1, 10, 11, 12)
class Solution {public: /* * param k : As description. * param n : As description. * return: How many k's between 0 and n. */ int digitCounts(int k, int n) { // write your code here int count = 0; for(int i = 0;i<=n;i++) { int number = i; while(number/10) { if(number % 10 == k) { count++; } number = number/10; } if(number == k) count++; } return count; }};
最终在网上找到的其他人的答案也是这个方法,如果有简单的方法希望告知。
0 0
- LintCode笔记(7)——统计数字
- Lintcode : 统计数字
- lintcode ----统计数字
- lintcode&九章算法——No.3 统计数字 ? 待解决
- lintcode-3-统计数字
- LintCode 3 统计数字
- LintCode 3 统计数字
- LintCode Python 统计数字
- [LintCode]3.统计数字
- LintCode 3.统计数字
- LintCode-(3)统计数字
- LintCode之3 统计数字
- Lintcode 3统计数字
- Lintcode 3. 统计数字
- lintcode 3:统计数字
- ZJNU——统计数字(1402)
- LintCode-剑指Offer-(3)统计数字
- (lintcode)第3题统计数字
- JavaScript--全选、全不选、反选、无刷新删除、批量删除、即点即改入库(在yii框架中操作)
- OPENCV入门教程二:opencv 2.4.13+VS2015开发环境配置
- Spring多数据源使用注解事务绑定注意事项和解决办法
- Greenplum利用gpload,gpfist实现数据入库
- 什么?创业者别光想着做CEO,他首先应该是一名出色的销售(一)
- LintCode笔记(7)——统计数字
- 算法导论第十三章 红黑树
- [实验室]2016.7.27
- yarn-site.xml配置参数
- HDU-5754 Life Winner Bo (博弈论)
- NSString, char*, NSData类型之间的转换
- React—native+Android环境搭建和开发配置(Mac)
- 【SSH快速进阶】——Spring AOP原理及其实现
- meta标签中的http-equiv属性介绍