//原题链接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. Shell - 集群监控脚本合集

    node_heart_check.sh #!/bin/bash scriptPath=$(dirname "$0") for ip in `cat /etc/hosts | gre ...

  2. Hive - [01] 概述

    一.Hive是什么 是Facebook开源,用于解决海量结构化日志的数据统计工具. 是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张表,并提供类SQL查询功能. Hive处理的数 ...

  3. c++经典卡常

    1.展开函数 如下代码: void lowbit(int x) { return x&(-x); } signed main() { cout << lowbit(12345); ...

  4. ABC393C题解

    大概评级:橙. 送分题. 题意就是让你统计有多少条边是重边或自环. 设 \(u_i\) 表示第 \(i\) 条边的左端点,\(v_i\) 表示第 \(i\) 条边的右端点. 那么如果 \(u_i = ...

  5. 洛谷P1191 矩形 题解

    笛卡尔树的介绍 笛卡尔树,是一种二叉搜索树,它满足如下条件: 每个节点的编号满足二叉搜索树的性质. 每个节点的权值满足小根堆或大根堆的性质. 大概是这个样子: 笛卡尔树的建树 请看这里. 笛卡尔树的用 ...

  6. mysql - 视图的操作 创建,修改,删除,查看

    只保存sql逻辑,不保存查询结果 视图可以看作是封装了多条sql语句,之后使用的时候就像普通表一样,而这个表上的字段则是创建视图时,select 后边跟的字段,支持列的别名. 创建 语法: creat ...

  7. 魔方求解器程序(层先法,java版本)

    实现了一个三阶魔方的层先法求解程序:https://github.com/davelet/java-puzzle-resolver 欢迎试用. 用法 1. 随机试用 不关注起始状态的话可以用程序的随机 ...

  8. python代码格式风格 PEP 8

    前言 Python Enhancement Proposal #8叫做PEP 8,它是针对 Python 代码格式而编订的风格指南. 编写 Python 代码时,总是应该遵循 PEP 8 风格指南. ...

  9. php7有哪些新特性

    目录 太空船操作符 标量类型声明和返回值的类型说明 null 合并操作符 常量数组 namespace 批量导入 非混合模式的 use 声明 混合模式的 use 声明 复合模式的 use 声明 thr ...

  10. Linux指令详解之:ctl相关命令大礼包

    目录 6.4 服务管理命令(ctl大礼包) 6.4.1 systemctl 6.5.2 systemctl小结 6.5.3 timedatectl 6.5.4 localectl 6.5.5 netw ...