zoukankan      html  css  js  c++  java
  • 233. 数字 1 的个数

    一、题解

    给定一个整数 n,计算所有小于等于 n 的非负整数中数字 1 出现的个数。

    示例 1:

    输入:n = 13
    输出:6
    示例 2:

    输入:n = 0
    输出:0
     

    提示:

    0 <= n <= 2 * 109

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/number-of-digit-one
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    二、题目难度:困难

    三、题解

    class Solution {
    public:
        int countDigitOne(int n) {
            int res = 0;
            for(long long i=1;i<=n;i *= 10){
                long long interval = i * 10;
                res += n/interval * i + min(i,max(0LL,n%interval-i+1));
            }
            return res;
        }
    };
  • 相关阅读:
    js原型杂谈
    arguments.callee.caller
    $resource
    sql的四种匹配模式
    AMD规范
    module.ngdoc
    angularjs杂谈
    浏览器前缀
    css21规范学习
    <meta>标签
  • 原文地址:https://www.cnblogs.com/ttzz/p/14561823.html
Copyright © 2011-2022 走看看