LeetCode OJ 之 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的数字的个数。从个位開始到最高位。

举个样例54215,比方如今求百位上的1。54215的百位上是2。能够看到xx100到xx199的百位上都是1,这里xx从0到54,即100->199, 1100->1199...54100->54199, 这些数的百位都是1,因此百位上的1总数是55*100

假设n是54125,这时因为它的百位是1,先看xx100到xx199。当中xx是0到53,即54*100, 然后看54100到54125,这是26个。所以百位上的1的总数是54*100 + 26.

假设n是54025,那么仅仅须要看xx100到xx199中百位上的1。这里xx从0到53,总数为54*100

求其它位的1的个数的方法是一样的。

代码:

class Solution {
public:
    int countDigitOne(int n)
    {
        int res=0;
        long left, right, base=1;
        if (n <= 0)
            return 0;
        while (n >= base)
        {
            left = n / base;   //left包括当前位
            right = n % base;  //right为当前位的右半边

            if ((left % 10) > 1)
                res+= (left / 10 + 1) * base;

            else if ((left % 10) == 1)
                res+= (left / 10) * base+ (right + 1);

            else
                res+= (left / 10) * base;
            base *= 10;
        }
        return res;
	}

};

能够把上面三个条件合成一步,例如以下:

class Solution {
public:
    int countDigitOne(int n)
    {
        int res=0;
        long left, right, base=1;
        if (n<=0)
            return 0;
        while (n>=base)
        {
            left = n / base;   //left包括当前位
            right = n % base;  //right为当前位的右半边

            res += ((left + 8) / 10 * base) + (left % 10 == 1) * (right + 1);
            base *= 10;
        }
        return res;
	}

};

原文地址:https://www.cnblogs.com/llguanli/p/8375844.html

时间: 2024-10-06 18:00:05

LeetCode OJ 之 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.

233 Number of Digit One 数字1的个数

给定一个整数 n,计算所有小于等于 n 的非负数中数字1出现的个数. 例如: 给定 n = 13, 返回 6,因为数字1出现在下数中出现:1,10,11,12,13. 详见:https://leetcode.com/problems/number-of-digit-one/description/ 方法一: class Solution { public: int countDigitOne(int n) { int cnt=0; for(long long m=1;m<=n;m*=10) {

[LeetCode OJ] Single Number之二 ——Given an array of integers, every element appears THREE times except for one. Find that single one.

1 class Solution { 2 public: 3 int singleNumber(int A[], int n) { 4 int bits = sizeof(int)*8; 5 int result=0; 6 for(int i=1; i<=bits; i++) 7 { 8 int w=0; 9 int t=1; 10 11 for(int j=0; j<n; j++) 12 w += (A[j]>>(i-1))&t; 13 result+= (w%3)<

[LeetCode OJ] Single Number之一 ——Given an array of integers, every element appears twice except for one. Find that single one.

1 class Solution { 2 public: 3 int singleNumber(int A[], int n) { 4 int i,j; 5 for(i=0; i<n; i++) 6 { 7 for(j=i+1; j<n; j++) 8 { 9 if(A[j]==A[i]) 10 { 11 int temp = A[i+1]; 12 A[i+1] = A[j]; 13 A[j] = temp; 14 i++; 15 break; 16 } 17 } 18 if(j==n) 19

【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. 代码: class Solution {

LeetCode 9 Palindrome Number(回文数字判断)

Long Time No See ! 题目链接https://leetcode.com/problems/palindrome-number/?tab=Description 首先确定该数字的位数.按照已知数字x对10进行多次求余运算,可以得到数字位数. 具体思路: 1.每次取出该数字的最高位和最低位进行比较. 2.如果不相等则直接返回FALSE, 3.如果相等修改x的值(去掉最高位也同时去掉最低位)其中去掉最高位可以通过求模运算,去掉最低位可以采用除以10 4.进行循环直到x的值不大于0为止

LeetCode OJ:Number of 1 Bits(比特1的位数)

Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should retu

&lt;LeetCode OJ&gt; 200. Number of Islands

Total Accepted: 48411 Total Submissions: 171609 Difficulty: Medium Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or verticall

LeetCode 268. Missing Number (缺失的数字)

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. For example,Given nums = [0, 1, 3] return 2. Note:Your algorithm should run in linear runtime complexity. Could you implement it usi