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

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.

 

public class Solution {
    public int countDigitOne(int n) {
        long res = 0;
        int left = n;
        int right = 0;
        int base = 1;
        while (left > 0) {
        	int cur = left % 10;
        	left = left / 10;
        	right = n % base;
        	if (cur == 1) {
        		res += left * base;
        		res += right + 1;
        	} else if (cur == 0) {
        		res += left * base;
        	} else {
        		res += (left+1) * base;
        	}
        	right = cur * base + right;
        	base = base * 10;
        }
        return (int) res;
    }
}

 

1
12
分享到:
评论

相关推荐

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

    COCI 2015 #1 Tasks

    If the card's number consists of only one digit, then `X = 0`. For example, the card with suit `P` and number 9 is labeled as `P09`. **Objective:** A complete deck has 52 cards in total, with ...

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

    Javascript-将人民币数字转换成大写形式.docx

    // Represent integral part of digit number. var decimal; // Represent decimal part of digit number. var outputCharacters; // The output result. var parts; var digits, radices, bigRadices, ...

    Int 到 Char 转换的 Java 程序.docx

    // Iterating the number taking one digit at a time for (int i = 0; i (); i++) { // Switch case // Reading digits one by one using charAt() method switch (s.charAt(i)) { case '1': System.out....

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

    JavaScript 动态将数字金额转化为中文大写金额

    // Represent integral part of digit number. var decimal; // Represent decimal part of digit number. var outputCharacters; // The output result. var parts; var digits, radices, bigRadices, ...

    机器学习 上

    8. "20.Ways to deal with the large number of possible outputs [15 min].mp4":在处理多分类问题时,课程可能会介绍一些策略,如softmax函数或one-hot编码,以应对大量可能的输出类别。 9. "23.Convolutional ...

    php-bnf-tree:根据 bnf 规则创建输入字符串树

    return $this->oneOf('digit', $this->sequence('digit', $this->method('number'))); } public function digit() { return $this->oneOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'); } } ``` 然后,...

Global site tag (gtag.js) - Google Analytics