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.
Solution:
For example '8192':
1-999 -> countDigitOne(999)
1000-1999 -> 1000 of 1s + countDigitOne(999)
2000-2999 -> countDigitOne(999)
.
.
7000-7999 -> countDigitOne(999)
8000-8192 -> countDigitOne(192)
Count of 1s : countDigitOne(999)*8 + 1000 + countDigitOne(192)
Noticed that, if the target is '1192':
Count of 1s : countDigitOne(999)*1 + (1192 - 1000 + 1) + countDigitOne(192)
(1192 - 1000 + 1) is the 1s in thousands from 1000 to 1192.
public int countDigitOne(int n) { if(n <= 0) return 0; if(n < 10) return 1; int base = (int)Math.pow(10, (n+"").length() - 1); int k = n / base; return countDigitOne(base - 1) * k + (k == 1 ? (n-base+1) : base) + countDigitOne(n % base); }
非递归的解法:
int countDigitOne(int n) { int result = 0; int digit = 1, num = n; while (num) { int mod = num % 10; int sign = mod > 0 ? 1 : 0; num /= 10; int a = num * digit; int b = sign * (mod == 1 ? n % digit + 1: digit); result += a + b; digit *= 10; } return result; }
相关推荐
《LeetCode 101 - A LeetCode Grinding Guide (C++ Version)》是一本专为C++程序员设计的深入解析LeetCode算法问题的指南。这本书采用彩色版,以直观的方式讲解了各种数据结构和算法,旨在帮助读者磨练编程技能,...
《LeetCode 101 - A LeetCode Grinding Guide (C++ Version)》是一本面向有一定C++编程基础,但缺乏刷题经验读者的教科书和工具书。作者高畅(Chang Gao)基于其在准备实习和秋招过程中对LeetCode题目的整理和刷题...
c c语言_leetcode 0017_letter_combinations_of_a_phone_number.zip
《LeetCode---C++实现》是一本专注于C++编程语言解决LeetCode在线判题平台上的算法问题的书籍。LeetCode是程序员广泛使用的平台,它提供了大量的编程题目来提升编程技能和算法理解,尤其对于准备面试的程序员来说,...
1. leetCode-126-Word-LadderII.md:这涉及到第126题,词梯问题,通常是一个字符串转换问题,目标是找到两个单词之间的最短转换序列,每次只改变一个字母。 2. leetcode-224-Basic-Calculator.md:这是第224题,基础...
c c语言_leetcode 0009_palindrome_number.zip
leetcode-cli-plugins leetcode-cli 的第 3 方插件。 什么是 如何使用 如何使用 插件 名称 描述 增强的命令 按公司或标签过滤问题 list 不要在同一台计算机上使 Chrome 的会话过期 login 不要在同一台计算机上使 ...
java java_leetcode-maximum-depth-of-binary-tree
java java_leetcode-111-minimum-depth-of-binary-tree
c c语言_leetcode 0004_median_of_two_sorted_array.zip
在LeetCode---Palindrom-Number-master这个压缩包中,很可能包含了这个问题的多个解法,包括但不限于Python、Java、C++等常见编程语言的实现。这些代码可以作为学习和参考的资源,帮助你更好地理解和掌握回文数的...
leetcode-cli 一个享受 leetcode 的高效 cli 工具! 非常感谢 leetcode.com,一个非常棒的网站! ⦙⦙⦙⦙⦙⦙⦙⦙ 一个很打问题的方法。 问题来缓解离线思考。 编码前的源代码。 居住和与 leetcode.com。 下载你...
leetcode-cli 注意:这个存储库是为了临时使用而分叉的。 注意:从 webbrowser 复制 cookie 并使用leetcode user -c可以临时修复不能。 一个享受 leetcode 的高效 cli 工具! 非常感谢 leetcode.com,一个非常棒的...
leetcode 接口 该项目可帮助您使用首选的 IDE 或带有命令行界面 (CLI) 的编辑器来执行 leetcode。 入门 先决条件 Windows 10、MacOS、Linux Chrome版 >=90.0.4430 安装 # Prepare your virtual environment conda ...
c c语言_leetcode 0030_substring_with_concatenation_of_all_words.zip
这个压缩包“leetcode--python”显然包含了与LeetCode相关的Python解题代码,可能是一个开源项目,用于存储用户或社区成员解决LeetCode问题的Python实现。 **LeetCode概述** LeetCode提供了一系列的算法和数据结构...
c c语言_leetcode 0019_remove_nth_node_from_end_of_list.zip
leetcode26 algo 算法与数据结构,练习代码 语言:java 8 开发工具:vscode,安装插件Java Extension Pack vscode有智能提示,可调试,有重构支持,满足代码练习要求,相比IDEA更轻量级,普通笔记本即可流畅运行。 ...
leetcode 答案Leetcode---算法 我对 Leetcode 算法问题的回答
leetcode-cli 一个享受 leetcode 的高效 cli 工具! 非常感谢 leetcode.com,一个非常棒的网站! ⦙⦙⦙⦙⦙⦙⦙⦙ 一个很打问题的方法。 问题来缓解离线思考。 编码前的源代码。 居住和与 leetcode.com。 下载你...