Lintcode Digit Counts

Source: Internet
Author: User
Tags lintcode

http://www.lintcode.com/en/problem/digit-counts/

Enumeration, there are three cases.

The first case is that the current bit is less than K, at which point the count on that bit depends on the value above that bit;

In the second case, the current bit equals K, at which point the count on that bit depends on the bit above and below that bit;

In the third case, where the current bit is greater than K, then the count of this bit depends on the high

Better think, here is the code

classSolution { Public:    /** param k:as description.     * param n:as description.     * Return:how many K ' s between 0 and N. */    intDigitcounts (intKintN) {//Write your code here        Long Long Base=1; intres =0;  while(Base<=N) {            intCur = (N/Base) %Ten; intHigh = n/Base/Ten; intLow = nBase; if(cur > k) res + = (High +1) *Base; Else if(cur = = k) Res + = (Low +1) + High *Base; ElseRes + = high *Base; Base*=Ten; }                returnRes; }};

Lintcode Digit Counts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.