Number of Digit One(Medium)
1.算法说明:
如3141592,在m(digitDivide)=100时,即要求计算百位上“1”的个数
其中a为31415,b为92,31415中出现了3142次“1”,因为每10个数里面出现1次“1”。而实际上,31415是3141500,所以把31415中1的个数再乘以m。如3141400~3141499中,前缀为31414的数出现了100次,所以需要乘以m(此时是100)。
class Solution {
public:
int countDigitOne(int n) { int ans=0;
for(long long digitDivide=1;digitDivide<=n;digitDivide*=10)
{
int a=n/digitDivide;
int b=n%digitDivide;
ans+=(a+8)/10*digitDivide+(a%10==1)*(b+1);
}
return ans; }};
Number of Digit One(Medium)的更多相关文章
- (medium)LeetCode 233.Number of Digit One
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- [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] Number of Digit Ones
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- Number of Digit One
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- Java for LeetCode 233 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 *HARD* -- 从1到n的整数中数字1出现的次数
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- 233. Number of Digit One
题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...
- 【LeetCode】233. Number of Digit One
题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...
- [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 ...
随机推荐
- [APIO2018]铁人两项(圆方树)
过了14个月再重新看这题,发现圆方树从来就没有写过.然后写了这题发现自己APIO2018打铁的原因竟然是没开long long,将树的部分的O(n)写挂了(爆int),毕竟去年APIO时我啥都不会,连 ...
- 落地即王道,锁死企业智变CP——云+AI
国庆前夜,"70年,我是主角"的微电影引发网络热议,这是人民日报新媒体和电影频道联手打造.京东云和京东AI提供技术支持.这是中国首部全民定制国庆献礼片,网友只要上传正脸照片,就能通 ...
- input框随输入的文字的多少自动调整宽度粗略版本
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- SQL 2005 带自增列 带外键约束 数据导入导出
1,生成建表脚本 选中要导的表,点右键-编写表脚本为-create到 ,生成建表脚本 2,建表(在新库),但不建外键关系 不要选中生成外键的那部分代码,只选择建表的代码 3,导数据,用SQL STU ...
- python:创建文件夹:写入文本1:读取txt:读取Excel文件遍历文件夹:
https://blog.csdn.net/u011956147/article/details/80369731 创建文件夹: import osimport shutil def buildfil ...
- node,npm,webpack,vue-cli模块化编程安装流程
首先什么都不要管,先装环境. pip是万能的!!! 安装node: pip3 install node 安装npm: pip3 install npm 安装webpack: npm install ...
- JavaScript—瀑布流
现在网页中需要翻页的列表,好多都已经改为瀑布流了.所以这个思路还是特别重要的 HTML Css 页面 因为每个图片的高度不一样所以她的 top 和left 我们待会通过JS计算 动态生成 js部分 思 ...
- Git&GitHub 基本使用
如果是Windows用户需要自行去Git的官网下载Git工具 相信以您的聪明才知不需要一会就可以安装好Git工具了 博主这里是MacOS系统,本省就自带Git工具就不演示安装了 在完成一个简单的Git ...
- Dojo Grid结合Ajax用法
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CustomerDefine ...
- keras中的一些小tips(一)
写这篇博客的原因主要是为了总结下在深度学习中我们常会遇到的一些问题,以及不知道如何解决,我准备把这个部分作为一个系列,为了让大家少走一些坑,对于本博客有什么错误,欢迎大家指出,下面切入正题吧. 1. ...