LeetCode-Number of Digit One(编程之美-1的数目)

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

For example:

Given n = 13,

Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.

题不难但是很难分析,首先我们想到的就是分解问题,找规律,把大问题小问题化,然后在编程解决。

很明显这里有一个比较好想的办法就是从1~n遍历,每次都去判断每个i中1的个数,显然比较慢。

肯定有别的办法,就是在给定n的时候直接分析。这个时候分析问题就可以从n=1开始尝试,用归纳法,找出什么规律出来。

很显然,我们可以按照位一个一个找,即在个位、十位、百位。。。出现过的1。

现在直接给出N的一般规律,比如N=abced五位数字的时候,我们分析百位c,有三种情况:

1)c == 0的时候,比如12013,此时百位出现1的是:00 100 ~ 00 199, 01 100~01 199,……,11 100~ 11 199,共1200个,显然这个有高位数字决定,并且受当前位数影响;

2)c == 1的时候,比如12113,此时百位出现1的肯定包括c=0的情况,另外还需要考虑低位的情况即:00100 ~ 00113共114个,

3)c >= 2的时候,比如12213,此时百位出现1的是:00 100 ~ 00 199, 01 100~01 199,……,11 100~ 11 199,12 100 ~ 12 199,共1300个,这个有高位数字决定,其实是加一,并且乘以当前位数

所以就可以写出如下代码:

    public int countDigitOne(int n) {
        long count = 0;
        long factor = 1;

        while (n / factor != 0) {
            long lower = n % factor;
            long cur = (n / factor) % 10;
            long higher = n / (factor * 10);

            if (cur < 2) {
                count += higher * factor;
                if (cur == 1) count += lower+1;
            } else {
                count += (higher+1) * factor;
            }
            factor *= 10;
        }
        return (int) count;
    }

分析这种问题确实比较费劲!

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-05 02:24:02

LeetCode-Number of Digit One(编程之美-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 than or equal to n. For example: Given n = 13, Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. Hint: Beware of overflow.

Leetcode: 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. For example: Given n = 13, Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. Hint: Beware of overflow.

[LeetCode]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. For example: Given n = 13, Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 解题思路 题目理解: 题目的意思是给定一个数

数字统计(LeetCode Number of digit One)

1.      一本书的页码从自然数1开始顺序编码直到自然数n.书的页码按照通常的习惯编排,每个页码都不含多余的前导数字0.例如第6页用6表示而不是06或 006.数字统计问题要求对给定书的总页码,计算出书的全部页码中分别用到多少次数字0,1,2,3,.....9. 思路:对于n位数,例如3位数,000-999,出现0,1,2...9的次数相同,记每个数字出现的次数为avg,这10个数字出现的总次数为sum=10*avg. 而n位数共有10^n个数,每个具体的数对于sum的贡献为n次. 得到:1

编程之美----1的数目

问题:1,写一个函数f(N),返回1到N之间出现的”1"的个数.2,满足条件“f(N)=N"的最大的N是多少? 解法:对于之间的每一个数字n,分情况讨论每一个数位出现1的个数,例如要计算百位上出现1的次数,它将会受到三个因素的影响:百位上的数字,百位以下(低位)的数字,百位(更高位)以上的数字.如果百位上的数字为0,可知,百位上可能出现1的次数由更高位决定,并且等于更高位数字*当前位数.如果百位上的数字为1,可知,百位上的数字可能出现1的次数不仅受更高位影响,还受低位影响.如果百位上数

leetcode&amp;编程之美——博文目录

leetcode刷题整理: 1——Two Sum(哈希表hashtable,map) 2——Add Two Numbers(链表) 3——Longest Substring Without Repeating Characters(set,哈希表,两个指针) 9——Palindrome Number (数学问题) 11——Container With Most Water(两个指针) 12——Integer to Roman(string,数学问题) 13——Roman to Integer(s

LeetCode 233. 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. For example:Given n = 13,Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 本来想凑一个关于阶乘的题目,做个笔记,不枉我昨天A八个

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 than or equal to n. For example: Given n = 13, Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 解题思路: 递归 static public in

[LeetCode][JavaScript]Number of Digit One

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. For example:Given n = 13,Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 数学题,真是为