LintCode 3.统计数字
来源:互联网 发布:php手机租车网站源码 编辑:程序博客网 时间:2024/06/03 07:57
计算数字k在0到n中的出现的次数,k可能是0~9的一个值
样例
例如n=12,k=1,在 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],我们发现1出现了5次 (1, 10, 11, 12)
public int digitCounts(int k, int n) { int count = 0; //循环从n->0,每个数和k比较 while(n >= 0){ count += isDigit(k,n); n--; } return count; } public static int isDigit(int k ,int n){ int time = 0; //记录单个数出现k的次数 while(n > 9){ if(n % 10 == k) // n%10得到最后一位,与k进行比较 time++; n /= 10; // n/10去掉最后一位 } if(n == k) //当n只剩一位时,与k比较 time++; return time; }
阅读全文
0 0
- [LintCode]3.统计数字
- LintCode 3.统计数字
- Lintcode 3. 统计数字
- Lintcode : 统计数字
- lintcode ----统计数字
- 【LintCode】3.统计数字(Medium)解题报告
- lintcode 3. 统计数字 纯数学解法
- lintcode-3-统计数字
- LintCode 3 统计数字
- LintCode 3 统计数字
- LintCode Python 统计数字
- LintCode-(3)统计数字
- LintCode之3 统计数字
- Lintcode 3统计数字
- lintcode 3:统计数字
- LintCode-剑指Offer-(3)统计数字
- (lintcode)第3题统计数字
- LintCode笔记(7)——统计数字
- GitBook 使用
- <Qt> 在connect中使用lambda实现高效的信号/槽关联
- Python学习-面向对象
- <Qt> QWidget派生类setStyleSheet方法无效问题
- 51nod 1089 最长回文子串 V2(Manacher算法)
- LintCode 3.统计数字
- <26>——Remove Duplicates from Sorted Array
- 跨域--window.name + iframe
- 基于QT的多线程视频监控的实现(二)
- ES6 函数扩展
- Redis常用命令
- 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回
- LvClass 的一个效率
- out内置对象