Skip to content

Commit 0b0d6ef

Browse files
committed
一刷3099
1 parent 211e22c commit 0b0d6ef

File tree

5 files changed

+47
-33
lines changed

5 files changed

+47
-33
lines changed

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21719,12 +21719,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
2171921719
//|Hard
2172021720
//|
2172121721

21722-
//|{counter:codes}
21723-
//|{leetcode_base_url}/harshad-number/[3099. Harshad Number^]
21724-
//|{source_base_url}/_3099_HarshadNumber.java[Java]
21725-
//|{doc_base_url}/3099-harshad-number.adoc[题解]
21726-
//|Easy
21727-
//|
21722+
|{counter:codes}
21723+
|{leetcode_base_url}/harshad-number/[3099. Harshad Number^]
21724+
|{source_base_url}/_3099_HarshadNumber.java[Java]
21725+
|{doc_base_url}/3099-harshad-number.adoc[题解]
21726+
|Easy
21727+
|
2172821728

2172921729
//|{counter:codes}
2173021730
//|{leetcode_base_url}/water-bottles-ii/[3100. Water Bottles II^]

docs/3099-harshad-number.adoc

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,33 @@
11
[#3099-harshad-number]
2-
= 3099. Harshad Number
2+
= 3099. 哈沙德数
33

4-
{leetcode}/problems/harshad-number/[LeetCode - 3099. Harshad Number ^]
4+
https://leetcode.cn/problems/harshad-number/[LeetCode - 3099. 哈沙德数 ^]
55

6-
An integer divisible by the *sum* of its digits is said to be a *Harshad* number. You are given an integer `x`. Return_ the sum of the digits _of_ _`x`_ _if_ _`x`_ _is a *Harshad* number, otherwise, return_ _`-1`_._
6+
如果一个整数能够被其各个数位上的数字之和整除,则称之为 *哈沙德数*Harshad number)。给你一个整数 `x` 。如果 `x`*哈沙德数* ,则返回 `x` 各个数位上的数字之和,否则,返回 `-1`
77

8-
9-
*Example 1:*
8+
*示例 1:*
109

11-
<div class="example-block">
12-
*Input:* <span class="example-io">x = 18
10+
****
11+
*输入:* [.example-io]#x = 18#
1312
14-
*Output:* <span class="example-io">9
13+
*输出:* [.example-io]#9#
1514
16-
*Explanation:*
15+
*解释:* `x` 各个数位上的数字之和为 `9` 。`18` 能被 `9` 整除。因此 `18` 是哈沙德数,答案是 `9` 。
16+
****
1717

18-
The sum of digits of `x` is `9`. `18` is divisible by `9`. So `18` is a Harshad number and the answer is `9`.
18+
*示例 2:*
1919

20+
****
21+
*输入:* [.example-io]#x = 23#
2022
21-
*Example 2:*
23+
*输出:* [.example-io]#-1#
2224
23-
<div class="example-block">
24-
*Input:* <span class="example-io">x = 23
25-
26-
*Output:* <span class="example-io">-1
27-
28-
*Explanation:*
29-
30-
The sum of digits of `x` is `5`. `23` is not divisible by `5`. So `23` is not a Harshad number and the answer is `-1`.
31-
32-
33-
34-
*Constraints:*
35-
36-
37-
* `1 <= x <= 100`
25+
*解释:* `x` 各个数位上的数字之和为 `5` 。`23` 不能被 `5` 整除。因此 `23` 不是哈沙德数,答案是 `+-1+` 。
26+
****
3827

28+
*提示:*
3929

30+
* `+1 <= x <= 100+`
4031
4132
4233
== 思路分析

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6282,7 +6282,7 @@ include::2850-minimum-moves-to-spread-stones-over-grid.adoc[leveloffset=+1]
62826282

62836283
// include::3098-find-the-sum-of-subsequence-powers.adoc[leveloffset=+1]
62846284

6285-
// include::3099-harshad-number.adoc[leveloffset=+1]
6285+
include::3099-harshad-number.adoc[leveloffset=+1]
62866286

62876287
// include::3100-water-bottles-ii.adoc[leveloffset=+1]
62886288

logbook/202503.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,10 @@ endif::[]
623623
|{doc_base_url}/0794-valid-tic-tac-toe-state.adoc[题解]
624624
|✅ 总结提炼判断规则。
625625

626+
|{counter:codes2503}
627+
|{leetcode_base_url}/harshad-number/[3099. 哈沙德数^]
628+
|{doc_base_url}/3099-harshad-number.adoc[题解]
629+
|✅ 数学计算
626630

627631
|===
628632

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _3099_HarshadNumber {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2025-05-08 16:47:40
8+
*/
9+
public int sumOfTheDigitsOfHarshadNumber(int x) {
10+
int sum = 0;
11+
int num = x;
12+
while (num > 0) {
13+
sum += num % 10;
14+
num /= 10;
15+
}
16+
return x % sum == 0 ? sum : -1;
17+
}
18+
// end::answer[]
19+
}

0 commit comments

Comments
 (0)