From 7507954a2d0a968b8c1b3be41e7d25847a34415b Mon Sep 17 00:00:00 2001 From: CyC2018 Date: Wed, 28 Oct 2026 13:45:06 +0800 Subject: [PATCH] =?UTF-8?q?Update=2043.=20=E4=BB=8E=201=20=E5=88=B0=20n=20?= =?UTF-8?q?=E6=95=B4=E6=95=B0=E4=B8=AD=201=20=E5=87=BA=E7=8E=B0=E7=9A=84?= =?UTF-8?q?=E6=AC=A1=E6=95=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 题目链接改为力扣中的对应题目,题目描述和代码按该题的接口调整。 --- notes/43. 从 1 到 n 整数中 1 出现的次数.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/notes/43. 从 1 到 n 整数中 1 出现的次数.md b/notes/43. 从 1 到 n 整数中 1 出现的次数.md index 89323cc8..2a947e23 100644 --- a/notes/43. 从 1 到 n 整数中 1 出现的次数.md +++ b/notes/43. 从 1 到 n 整数中 1 出现的次数.md @@ -1,14 +1,14 @@ # 43. 从 1 到 n 整数中 1 出现的次数 -[NowCoder](https://www.nowcoder.com/practice/bd7f978302044eee894445e244c7eee6?tpId=13&tqId=11184&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github) +[Leetcode](https://leetcode.cn/problems/1nzheng-shu-zhong-1chu-xian-de-ci-shu-lcof/) ## 解题思路 ```java -public int NumberOf1Between1AndN_Solution(int n) { +public int digitOneInNumber(int num) { int cnt = 0; - for (int m = 1; m <= n; m *= 10) { - int a = n / m, b = n % m; + for (long m = 1; m <= num; m *= 10) { + long a = num / m, b = num % m; cnt += (a + 8) / 10 * m + (a % 10 == 1 ? b + 1 : 0); } return cnt;