`
songlj
  • 浏览: 15942 次
社区版块
存档分类
最新评论

Number of Digit One

 
阅读更多

解题思路:0-91的个数为1个,而X99..91的个数可以通过公式计算:即Math.pow(10, digit-1)+(10-(9-highestDigit))*Math.pow(10,digit-2)*(digit-1);比如899910^3+9*10^2*3。由此可以将输入数字分解,比如9548=9000+548=9000+500+48=9000+500+40+8,如此分布计算即可

Java代码:

public class Solution {
    public int countDigitOne(int n) {
		int count=0;
        while(n>0){
        	if(n/10==0) {
        		count++;
        		return count;//大于1的个位数只有一个1,返回
        	}
        	int digit=0;//存储数字位数
        	int num=n;
        	while(num/10>0){
        		digit++;
        		num=num/10; //num最高位数字		
        	}
        	digit++;        	
        	int highestDigit=num;  //最高位数字
        	num=(int) (num*(Math.pow(10, digit-1)))-1; 
        	if(highestDigit==1) {//1为最高位时,-1数字位数将会发生改变
        		count++;
        		count+=n-(num+1);
        		highestDigit=9;
        		digit--;
        	}
        	else{
        		highestDigit--;
        	}        	    
        	count+=Math.pow(10, digit-1)+(10-(9-highestDigit))*Math.pow(10, digit-2)*(digit-1);//如8999计算1的公式
        	n=n-(num+1);
        }
        return count;
    }
}
原题题目:https://leetcode.com/problems/number-of-digit-one/




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

分享到:
评论

相关推荐

    Andy619-Zhu#CS-Notes-chu#43. 从 1 到 n 整数中 1 出现的次数1

    43. 从 1 到 n 整数中 1 出现的次数解题思路Leetcode : 233. Number of Digit One。

    Multi-digit Number Recognition from Street View Imagery using DCNN

    millions of street number annotations and achieve over 90% accuracy. To further explore the applicability of the proposed system to broader text recognition tasks, we apply it to transcribing ...

    LeetCode最全代码

    The number of questions is increasing recently. Here is the classification of all `468` questions. For more questions and solutions, you can see my [LintCode](https://github.com/kamyu104/LintCode) ...

    Simulation models with correct statistical properties

    if the number of sinusoids is as small as a single-digit integer. More-over, the probability density functions of the envelope and phase, the level crossing rate, the average fade duration, and the ...

    nim_组合数学

    Then, the Nim position is losing if and only if, among all the ki’s, there are an even number of 1’s in each digit position. In other words, the Nim position is losing if and only if the xor of the...

    Huffman Coding

    the length of the coded message in terms of number of bits, Final Output (Example Values Not Accurate) Symbol frequency A, 15% m, 11% 7, 6% Symbol Huffman Codes A, 10101 m, 1101 7, 111 Total ...

    数位板压力测试

    The cursor must be able to return at least one bit of additional state (via a but¬ton, touching a digitizing surface, etc.). Devices may have multiple cursor types that have different physical ...

    EEUPDATE刷MAC工具 EEUPDATE v5.30.10.00

    4 hex digit subsystem device id of card to program. /DUMP Dumps EEPROM/Shadow RAM memory contents to file *.eep and flash memory to *.bin (if available) /MAC_DUMP_FILE Dumps the MAC address to a ...

    VB编程资源大全(英文源码 其它)

    prngmit.zip This Random Number Generator is for everybody who is tired of using the "Randomize" and "Rnd" in VB and VBScript and having to watch the SAME "Random" numbers appear (The problem is ...

    PLSQL.Developer v11.0.0.1762 主程序+ v10中文包+keygen

    Large number values are now formatted with digit groups Table Definition Editor enhancements Support added for identity columns (Oracle 12c) Support added for "default on null" columns (Oracle 12c) ...

    abstract:使用Go生成抽象语法树

    operator := OneOf ( plus , minus , times , over ) number := And ( spaces , Many ( Digit ), spaces ). Alias ( "number" ) mainlexer := And ( Many ( And ( number , operator )), number ) result :=

    eac3to V3.17

    * fixed: "1:some.ac3" instead of "1: some.ac3" failed for 2 digit track numbers * fixed: "eac3to source movie.mkv" demuxed video instead of muxing to MKV * negative edit now begins at the specified ...

    建伍378G写频软件

    Select one of the pre-programmed Signalling formats. Press the up- or down-arrow keys to increase or decrease the Signalling Tone number or enter the desired Signalling Tone number with the keyboard,...

    qemu-hdl-cosim:带有PCIe连接的FPGA的服务器的VM-HDL协同仿真

    使用工具此发行版已经使用以下工具和库进行了测试: Ubuntu 18.04.3Vivado 2018.3QEMU 2.10 rc3要设置的环境变量 COSIM_REPO_HOME -> Root of the source releaseCOSIM_PORT -> Any 4-digit number. If you want to ...

    Bochs - The cross platform IA-32 (x86) emulator

    - Fix BIOS INT13 function 08 when the number of cylinders on the disk = 1 - I/O Devices - USB HP DeskJet 920C printer device emulation (Ben Lunt) - Misc - Updated Bochs TESTFORM to version 0.5 -...

    联想旭日420AXP系统芯片组驱动

    2. One of the following operating systems must be fully installed and running on the system before installing this software: Microsoft Windows* Server 2003 with Service Pack 1 Microsoft Windows ...

    c++操作Excel的一个动态库程序

    //function: merge more than one cells into one cell //parameter: fileID ->the excel file ID which we will merge the cell,between 1 and 256 // sheetID ->the sheet index of the excel file which we will...

    一个win32下的ARM开源编译器

    1) label names cannot begin with a digit 2) CPSIE and CPSID formats are changed, use "iflags_aif" form instead of "aif" (eg. "CPSIE iflags_i" instead of "CPSID i") 3) SRS with writeback must have a ...

    一个java正则表达式工具类源代码.zip(内含Regexp.java文件)

    * \p{Punct} Punctuation: One of !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ \p{Punct} 标点符号: 包括 !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ * \p{Graph} A visible character: [\p{Alnum}\p{Punct}] \p{Graph}...

    联想旭日420AXP系统声卡驱动

    ;Windows 2000/XP WDM Modem Setup File ;... 2004-2006 ;Manufacturer: Motorola Inc [Version] Signature = "$Windows NT$" Class = Modem ClassGUID = {4D36E96D-E325-11CE-BFC1-08002BE10318} ...

Global site tag (gtag.js) - Google Analytics