LeetCode() 数字1的个数
int ones = 0;
for (long m = 1; m <= n; m *= 10) {
long a = n/m, b = n%m;
ones += (a + 8) / 10 * m;
if(a % 10 == 1) ones += b + 1;
}
return ones;
intuitive: 每10个数, 有一个个位是1, 每100个数, 有10个十位是1, 每1000个数, 有100个百位是1. 做一个循环, 每次计算单个位上1得总个数(个位,十位, 百位).
例子:
以算百位上1为例子: 假设百位上是0, 1, 和 >=2 三种情况:
case 1: n=3141092, a= 31410, b=92. 计算百位上1的个数应该为 3141 *100 次.
case 2: n=3141192, a= 31411, b=92. 计算百位上1的个数应该为 3141 *100 + (92+1) 次.
case 3: n=3141592, a= 31415, b=92. 计算百位上1的个数应该为 (3141+1) *100 次.
以上三种情况可以用 一个公式概括:
(a + 8) / 10 * m + (a % 10 == 1) * (b + 1);LeetCode() 数字1的个数的更多相关文章
- Leetcode 233.数字1的个数
		数字1的个数 给定一个整数 n,计算所有小于等于 n 的非负整数中数字 1 出现的个数. 示例: 输入: 13 输出: 6 解释: 数字 1 出现在以下数字中: 1, 10, 11, 12, 13 . ... 
- Java实现 LeetCode 233 数字 1 的个数
		233. 数字 1 的个数 给定一个整数 n,计算所有小于等于 n 的非负整数中数字 1 出现的个数. 示例: 输入: 13 输出: 6 解释: 数字 1 出现在以下数字中: 1, 10, 11, 1 ... 
- 计算1到N中包含数字1的个数
		转自:http://pandonix.iteye.com/blog/204840 Mark N为正整数,计算从1到N的所有整数中包含数字1的个数.比如,N=10,从1,2...10,包含有2个数字1. ... 
- [LeetCode] Number of Digit One 数字1的个数
		Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ... 
- LeetCode OJ 之 Number of Digit One (数字1的个数)
		题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ... 
- leetcode 233. 数字 1 的个数
		问题描述 给定一个整数 n,计算所有小于等于 n 的非负整数中数字 1 出现的个数. 示例: 输入: 13 输出: 6 解释: 数字 1 出现在以下数字中: 1, 10, 11, 12, 13 . 问 ... 
- [CareerCup] 18.4 Count Number of Two 统计数字2的个数
		18.4 Write a method to count the number of 2s between 0 and n. 这道题给了我们一个整数n,让我们求[0,n]区间内所有2出现的个数,比如如 ... 
- [Swift]LeetCode233. 数字1的个数 | Number of Digit One
		Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ... 
- 233 Number of Digit One 数字1的个数
		给定一个整数 n,计算所有小于等于 n 的非负数中数字1出现的个数. 例如: 给定 n = 13, 返回 6,因为数字1出现在下数中出现:1,10,11,12,13. 详见:https://leetc ... 
随机推荐
- svn server
			svn server: 1.c:\Program Files\TortoiseSVN\bin>svnserve -d -r C:\Jasper\Repositories2.change the ... 
- iOS FMDB的使用
			简介: SQLite (http://www.sqlite.org/docs.html) 是一个轻量级的关系数据库.iOS SDK 很早就支持了 SQLite,在使用时,只需要加入 libsqlite ... 
- pycharm 常用设置,打开文件数量
			1.主题(皮肤)和字体的设置(暂时略过) File ->Settings -> 搜索Font 2.文档行号的设置 1,临时设置.右键单击行号处,选择 Show Line Numbers. ... 
- Filestream(读写)
			using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ... 
- [转]Jenkins Xcode打包ipa
			本地打包. 如果Mac 上没有安装brew.先安装:ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/ins ... 
- linux 下mysql的启动 、调试、排错
			Linux 下 MySQL 启动与关闭 说明 一.启动 1.1 MySQL 进程 可以用ps 命令查看进程: [root@rac2 ~]# ps -ef|grep mysql root 21 ... 
- 总结七条助你成为Linux高手的超棒忠告
			起初Linux对于我来说其实是很纠结的,因为很早以前就听说过.也曾见各种技术大牛使用过,但是一直觉得非常高深而没有去正式接触.两年前随着自己工作愈发的乏味,又看到了一篇叫做"虽然我是医生,但 ... 
- Windows下gvim配置
			Windows下gvim配置原作地:http://hi.baidu.com/leemoncc/blog/item/a6be15cf40d7ab31b600c806.html 0.准备软件及插件. (a ... 
- 项目中必须知道的关于CSS+DIV的常识
			根据模块化的思想,将目录划分为html,css,image三大部分. css部分:(base.css.globa.css和mod文件夹)1.base.css放置的是reset,clearfix等基础类 ... 
- iOS移动下上传图片失败解决 (上传多图,带其他参数)
			项目中有一个主要的功能,就是上传图片,结结果移动真的是很奇怪,WiFi,联通,电信都没有问题的情况下,居然在移动下不行,真的是很头疼.不过好在最后是解决了 项目的网络请求我是采用ASIHttpRequ ... 
