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;
}
};
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 th ...
- 233 Number of Digit One 数字1的个数
给定一个整数 n,计算所有小于等于 n 的非负数中数字1出现的个数. 例如: 给定 n = 13, 返回 6,因为数字1出现在下数中出现:1,10,11,12,13. 详见:https://leetc ...
- 【LeetCode】233. Number of Digit One
题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...
- LeetCode 246. Strobogrammatic Number (可颠倒数字) $
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- LeetCode 9. Palindrome Number (回文数字)
Determine whether an integer is a palindrome. Do this without extra space. 题目标签:Math 题目给了我们一个int x, ...
- [LeetCode] 137. Single Number II 单独的数字之二
Given a non-empty array of integers, every element appears three times except for one, which appears ...
- LeetCode OJ -Happy Number
题目链接:https://leetcode.com/problems/happy-number/ 题目理解:实现isHappy函数,判断一个正整数是否为happy数 happy数:计算要判断的数的每一 ...
- LeetCode 268. Missing Number (缺失的数字)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- LeetCode 9 Palindrome Number(回文数字判断)
Long Time No See ! 题目链接https://leetcode.com/problems/palindrome-number/?tab=Description 首先确定该数字的 ...
随机推荐
- [Windows Server]安装系统显示“缺少计算机所需的介质驱动程序”解决方案
1.把电脑上插着的硬盘拔了 2.重试 3.修复计算机找到dos命令行 4.然后进入我们放置解压了的系统的那个符盘,(我这里放在D盘)输入:d: 找到刚才我们解压了的系统文件,进入sourc ...
- OJ刷题---手机尾号评分
题目要求: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dis ...
- cocos2dx项目创建
射击类游戏文档 作者:浙江传媒学院 新媒体 张勇 1>编译环境 首先我们先去cocos2dx官网上下载cocos2dx最新版本号 http://www.cocos2d-x.org/ 我下载的 ...
- 安卓系统底层C语言算法之測试參数是几个long型的算法
#include <stdio.h> #define BITS_PER_LONG (sizeof(unsigned long) * 8) //求一个数x是几个long的长度 #define ...
- cocos2d-x 3.0游戏实例学习笔记《卡牌塔防》第一步---開始界面&关卡选择
/* 说明: **1.本次游戏实例是<cocos2d-x游戏开发之旅>上的最后一个游戏,这里用3.0重写并做下笔记 **2.我也问过木头本人啦.他说:随便写,第一别全然照搬代码:第二能够说 ...
- POJ3249 Test for Job(拓扑排序+dp)
Test for Job Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10137 Accepted: 2348 Des ...
- 【SDOI 2016】 排列计数
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4517 [算法] 有m个数在原来的位置上,说明有(n-m)个数不再原来的位置上 那么, ...
- 12.Matlab神经网络工具箱
概述: 1 人工神经网络介绍 2 人工神经元 3 MATLAB神经网络工具箱 4 感知器神经网络 5 感知器神经网络 5.1 设计实例分析 clear all; close all; P=[ ; ]; ...
- js 转化为几天前,几小时前,几分钟前...
转换标准时间为时间戳: function getDateTimeStamp(dateStr){ return Date.parse(dateStr.replace(/-/gi,"/" ...
- QT-helloworld-Qt设计师编写
前言:Qt设计师界面类就是C++类和ui文件的结合,它将这两个文件一起生成了,而不用再逐一添加. 目标:在对话框中显示出“helloworld”字样. 一.新建项目 1.1 选择项目模板 文件→新建文 ...