//原题链接https://leetcode.com/problems/number-of-digit-one/

  • 题目描述

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

    Example:

    Input: 13
    Output: 6
    Explanation: Digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.
  • 思路分析
    1.暴力法:对10求余,判断个位是否为1,然后除10依次判断//会超时
    2.优化:总结n每位数的规律,
    以n=123为例
       (1的个数:count
       当前位数上的权重:weight
       当前位数上的数字:now
       上一位数:last,例如按照从右到左,2的上一位数为3
       循环次数:round)
    个位数:3属于大于等于1,第十三个循环开始,则count = round + 1 = 12+1;
                  若为120,即now为小于1,开始第十三个循环,则count = round=12;
    十位数及以上位数:以十位数为例,2大于1,则count = round*weight + weight=1*10+10
                                                            若为11X,即now为1,则count = round*weight + 1+last=1*10+1+x
                                                            若为10X,即now等于0,则count=round*weight=1*10
  • 源码附录
     
    class Solution {
    public int countDigitOne(int n) {
    if(n<1){
    return 0;
    } int count = 1;
    for(int i=0;i<=n;i++){
    while(i>0){
    if(i%10 == 1){
    count ++;
    }
    i = i / 10;
    }
    }
    return count;
    }
    }
    class Solution {
    public int countDigitOne(int n) {
    if(n<1){
    return 0;
    } int count = 0;
    int round = n;
    int weight = 1;
    int now = 0 ;
    while(round>0){
    now = round%10;
    round = round/10;
    if(now==1){
    count = count + (n%weight)+1;
    }
    else if(now>1){
    count = count + weight;
    }
    count = count + round*weight;
    weight = weight*10;
    }
    return count;
    }
    }

Number of Digit One——LeetCode⑩的更多相关文章

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

  2. [Leetcode] Number of Digit Ones

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

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

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

  5. 【LeetCode】233. Number of Digit One

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

  6. LeetCode 233 Number of Digit One 某一范围内的整数包含1的数量

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

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

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

  10. Number of Digit One

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

随机推荐

  1. ‌PCI-5565PIO主要应用场景

    ‌PCI-5565PIO主要应用场景包括军事领域.工业自动化和控制系统.仿真与培训以及数据采集与分发‌.在军事领域,PCI-5565PIO可用于航空航天系统的飞行控制计算机.导航系统和传感器系统之间的 ...

  2. 【忍者算法】从快慢指针到倒数查找:优雅解决链表倒数问题|LeetCode第19题"删除链表的倒数第N个结点"

    从快慢指针到倒数查找:优雅解决链表倒数问题 从生活场景说起 想象你在一个漫长的队伍中,想知道自己距离队尾还有多少人.一个巧妙的方法是:让你的朋友从你所在位置往后数N步,然后你和朋友一起向后走.当朋友走 ...

  3. NCS开发学习笔记-基础篇-第 1 课 – 安装 nRF Connect SDK 开发环境

    练习1 -安装 nRF Connect SDK 开发环境 需要安装的软件 Git python J-Link nrfutil nRF Command Line Tools VScode Chinese ...

  4. 牛逼了!16.2K Star!推荐一款开源的网络爬虫和浏览器自动化库:Crawlee!

    在当今的互联网世界中,网络爬虫作为一种重要的工具,被广泛应用于数据收集.内容监控.SEO优化以及自动化测试等多个领域.随着技术的不断进步,各种开源的网络爬虫库也应运而生.今天,我向大家推荐一款非常优秀 ...

  5. @SpringBootApplication自动配置原理

    @EnableAutoConfiguration 是核心,他会调用一个@Import注解.我们已知Import自动配置得实现是通过创建ImportSelector 接口的实现类并重写里面selectI ...

  6. Ubuntu22.04双网卡调试

    最近捡起正点原子的linux开发板,又开始了linux的学习,这条路走走停停的,隔了一年时间很多积累的东西都忘了.打开VMware虚拟机发现网络也连接不上了,我的印象中去年是把虚拟机的双网卡配置好了, ...

  7. C#语言碎片:Switch-Case语句字符串匹配

    Switch case语句在处理字符串类型匹配时候,case条件需要设置为静态常量或者一个具体的字符串: 因为工具类ToolHand.Name 为变量,所以编译不通过. 使用if语句来逐个判断: 看A ...

  8. linux xxx is not in the sudoers file. This incident will be reported.

    前言 linux 报错:xxx is not in the sudoers file. This incident will be reported. 这意味着用户 xxx 没有在 sudoers 文 ...

  9. SQLSTATE[HY000] [2002] Connection refused报错 PHP连接docker容器中的mysql

    Laradock 是基于 Docker 提供的完整 PHP 本地开发环境 在框架中连接 MySQL 时 报错 SQLSTATE[HY000] [2002] Connection refused 主要还 ...

  10. bug|electron-vue 使用 electron-builder 打包,执行 yarn run build 报错原因

    问题 & 解决 官方BUG:tasks 重复: yarn run build yarn run v1.22.22 $ node .electron-vue/build.js && ...