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)的更多相关文章

  1. (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 ...

  2. [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 ...

  3. [Leetcode] Number of Digit Ones

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

  4. Number of Digit One

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

  5. 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 ...

  6. 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 ...

  7. 233. Number of Digit One

    题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...

  8. 【LeetCode】233. Number of Digit One

    题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...

  9. [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 ...

随机推荐

  1. java 简单的冒泡

    import java.util.Arrays; public class mao { public static void main(String[] args) { int [] array={1 ...

  2. mariabd mysql升级mariadb

    还有错误 [root@localhost /]# mysqldump --all-databases --user=root --password --master-data > backupd ...

  3. 【动手学pytorch】pytorch的基础操作

    一.Tensor a)       张量是torch的基础数据类型 b)       张量的核心是坐标的改变不会改变自身性质. c)        0阶张量为标量(只有数值,没有方向的量),因为它不随 ...

  4. Intellij IDEA破解方法

    1.破解(参考网站) http://idea.lanyus.com/ 2. Intellij idea使用教程 https://github.com/tengj/IntelliJ-IDEA-Tutor ...

  5. Java IO流操作 (II)

    缓冲流 /* * BufferedWriter:将文本写入字符输出流,缓冲各个字符,从而提供单个字符.数组和字符串的高效写入. * BufferedReader:从字符输入流中读取文本,缓冲各个字符, ...

  6. ES6 之 第七种数据类型Symbol

    概述 为了减少对象的属性名冲突,ES6引入新的原始数据类型Symbol,JS的第七种数据类型. Symbol 能够保证每个属性的名字都是独一无二,这样就能从根本上防止属性名冲突. Symbol 值能够 ...

  7. 《Java核心技术卷I》观赏指南

    Tomxin7 如果你有想看书的计划,但是还在纠结哪些书值得看,可以简单看看"观赏指南"系列,本文会简单列出书中内容,给还没有买书的朋友提供一个参考. 前言 秋招过去很久了,虽然在 ...

  8. docker入门1---docker的简介和安装

    Tomxin7 Simple, Interesting | 简单,有趣 什么是Docker? 简介: Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的.可移植的.自给自足的容器.开发 ...

  9. MySQL--SQL分类

    SQL语句主要可以划分为以下3个类别: DDL(Data Definition Languages)语句:数据定义语言,这些语句定义了不同的数据段.数据库.表.列.索引等数据库对象. 常用的语句关键字 ...

  10. 从Evernote大批顶尖高管离职,看处于漩涡中的笔记应用未来前景

    无论是巨头,还是独角兽,甚至是小而美的某些企业,在发生高管离职.裁员等情况时,总会引起业界的广泛关注.究其原因,就在于高管离职.裁员等往往意味着企业内部发生了动荡,甚至还会直接反映出所在行业的发展趋势 ...