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

本题目纯粹是找规律求解。

该实现参考了:

//Solution:
//For example ‘8192‘:
//
//1-999 -> countDigitOne(999)
//1000-1999 -> 1000 of 1s + countDigitOne(999)
//2000-2999 -> countDigitOne(999)
...
//7000-7999 -> countDigitOne(999)
//
//8000-8192 -> countDigitOne(192)
//
//Count of 1s : countDigitOne(999)*8 + 1000 + countDigitOne(192)
//
//Noticed that, if the target is ‘1192‘:
//
//Count of 1s : countDigitOne(999)*1 + (1192 - 1000 + 1) + countDigitOne(192)
//
//(1192 - 1000 + 1) is the 1s in thousands from 1000 to 1192.

链接:
https://leetcode.com/discuss/44496/5lins-solution-using-recursion

实现代码:

    public int CountDigitOne(int n) {

    if(n <= 0){
		return 0;
	}
    if(n < 10){
		return 1;
	}

	var result = 0;
    var digit = 1;
	var num = n;
    while (num > 0) {
        var mod = num % 10;
        var sign = mod > 0 ? 1 : 0;  

        num /= 10;
        int a = num * digit;
        int b = sign * (mod == 1 ? n % digit + 1: digit);
        result += a + b;
        digit *= 10;
    }
    return result;
    }

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

时间: 2024-10-27 10:06:25

LeetCode -- Count Digit One的相关文章

LeetCode: Count and Say [037]

[题目] The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is read off as "one 2, then one 1" or 1211. G

leetcode 之digit

leetcode 1067. Digit Count in Range leetcode 233. Number of Digit One leetcode 902. Numbers At Most N Given Digit Set leetcode 1088. Confusing Number II 都是求<=N的数中满足某种条件的数字数量 leetcode 1067. Digit Count in Range leetcode 233. Number of Digit One 这不单单是求

LeetCode: Count and Say 题解

The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2, then one 1" or 1211. Given an

[LeetCode]Count Primes

题目:Count Primes 统计1-n的素数的个数. 思路1: 通常的思想就是遍历(0,n)范围内的所有数,对每个数i再遍历(0,sqrt(i)),每个除一遍来判断是否为素数,这样时间复杂度为O(n*sqrt(n)). 具体实现不在贴代码,过程很简单,两重循环就可以解决.但是效率很差,n较大时甚至会花几分钟才能跑完. 思路2: 用埃拉特斯特尼筛法的方法来求素数,时间复杂度可以达到O(nloglogn). 首先开一个大小为n的数组prime[],从2开始循环,找到一个质数后开始筛选出所有非素数

[LeetCode]Count of Range Sum

题目:Count of Range Sum Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Range sum S(i, j) is defined as the sum of the elements in nums between indices i and j (i ≤ j), inclusive. Note:A naive algorithm

LeetCode Count Univalue Subtrees

原题链接在这里:https://leetcode.com/problems/count-univalue-subtrees/ Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of the subtree have the same value. For example:Given binary tree, 5 / 1 5 / \ 5 5 5 retur

[LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Example: Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99]) Hint: A direct

leetcode Count of Smaller Numbers After Self

题目连接 https://leetcode.com/problems/count-of-smaller-numbers-after-self/ Count of Smaller Numbers After Self Description You are given an integer array nums and you have to return a new counts array. The counts array has the property where $counts[i]$

Leetcode: Nth Digit

Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note: n is positive and will fit within the range of a 32-bit signed integer (n < 231). Example 1: Input: 3 Output: 3 Example 2: Input: 11 Output: 0 Explanatio