Skip to content

Commit e62ce5f

Browse files
committed
一刷3115
1 parent c53dd60 commit e62ce5f

File tree

5 files changed

+79
-32
lines changed

5 files changed

+79
-32
lines changed

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21831,12 +21831,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
2183121831
//|Easy
2183221832
//|
2183321833

21834-
//|{counter:codes}
21835-
//|{leetcode_base_url}/maximum-prime-difference/[3115. Maximum Prime Difference^]
21836-
//|{source_base_url}/_3115_MaximumPrimeDifference.java[Java]
21837-
//|{doc_base_url}/3115-maximum-prime-difference.adoc[题解]
21838-
//|Medium
21839-
//|
21834+
|{counter:codes}
21835+
|{leetcode_base_url}/maximum-prime-difference/[3115. Maximum Prime Difference^]
21836+
|{source_base_url}/_3115_MaximumPrimeDifference.java[Java]
21837+
|{doc_base_url}/3115-maximum-prime-difference.adoc[题解]
21838+
|Medium
21839+
|
2184021840

2184121841
//|{counter:codes}
2184221842
//|{leetcode_base_url}/kth-smallest-amount-with-single-denomination-combination/[3116. Kth Smallest Amount With Single Denomination Combination^]

docs/3115-maximum-prime-difference.adoc

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,42 @@
11
[#3115-maximum-prime-difference]
2-
= 3115. Maximum Prime Difference
2+
= 3115. 质数的最大距离
33

4-
{leetcode}/problems/maximum-prime-difference/[LeetCode - 3115. Maximum Prime Difference ^]
4+
https://leetcode.cn/problems/maximum-prime-difference/[LeetCode - 3115. 质数的最大距离 ^]
55

6-
You are given an integer array `nums`.
6+
给你一个整数数组 `nums`
77

8-
Return an integer that is the *maximum* distance between the *indices* of two (not necessarily different) prime numbers in `nums`_._
8+
返回两个(不一定不同的)质数在 `nums`*下标**最大距离*
99

10-
11-
*Example 1:*
10+
*示例 1:*
1211

13-
<div class="example-block">
14-
*Input:* <span class="example-io">nums = [4,2,9,5,3]
12+
****
13+
*输入:* [.example-io]#nums = [4,2,9,5,3]#
1514
16-
*Output:* <span class="example-io">3
15+
*输出:* [.example-io]#3#
1716
18-
*Explanation:* `nums[1]`, `nums[3]`, and `nums[4]` are prime. So the answer is `|4 - 1| = 3`.
17+
*解释:* `nums[1]`、`nums[3]` 和 `nums[4]` 是质数。因此答案是 `|4 - 1| = 3`。
18+
****
1919

20+
*示例 2:*
2021

21-
*Example 2:*
22+
****
23+
*输入:* [.example-io]#nums = [4,8,2,8]#
2224
23-
<div class="example-block">
24-
*Input:* <span class="example-io">nums = [4,8,2,8]
25+
*输出:* [.example-io]#0#
2526
26-
*Output:* <span class="example-io">0
27-
28-
*Explanation:* `nums[2]` is prime. Because there is just one prime number, the answer is `|2 - 2| = 0`.
29-
30-
31-
32-
*Constraints:*
33-
34-
35-
* `1 <= nums.length <= 3 * 10^5^`
36-
* `1 <= nums[i] <= 100`
37-
* The input is generated such that the number of prime numbers in the `nums` is at least one.
27+
*解释:* `nums[2]` 是质数。因为只有一个质数,所以答案是 `|2 - 2| = 0`。
28+
****
3829

30+
*提示:*
3931

32+
* `1 \<= nums.length \<= 3 * 10^5^`
33+
* `+1 <= nums[i] <= 100+`
34+
* 输入保证 `nums` 中至少有一个质数。
4035
4136
4237
== 思路分析
4338

39+
从数组前后分别寻找质数,然后坐标相减即可。
4440

4541
[[src-3115]]
4642
[tabs]

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6314,7 +6314,7 @@ include::3099-harshad-number.adoc[leveloffset=+1]
63146314

63156315
// include::3114-latest-time-you-can-obtain-after-replacing-characters.adoc[leveloffset=+1]
63166316

6317-
// include::3115-maximum-prime-difference.adoc[leveloffset=+1]
6317+
include::3115-maximum-prime-difference.adoc[leveloffset=+1]
63186318

63196319
// include::3116-kth-smallest-amount-with-single-denomination-combination.adoc[leveloffset=+1]
63206320

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,11 @@ endif::[]
638638
|{doc_base_url}/0144-binary-tree-preorder-traversal.adoc[题解]
639639
|✅ 树的 Morris 遍历
640640

641+
|{counter:codes2503}
642+
|{leetcode_base_url}/maximum-prime-difference/[3115. 质数的最大距离^]
643+
|{doc_base_url}/3115-maximum-prime-difference.adoc[题解]
644+
|✅ 质数判断
645+
641646
|===
642647

643648
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _3115_MaximumPrimeDifference {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2025-05-09 21:44:53
9+
*/
10+
public int maximumPrimeDifference(int[] nums) {
11+
int min = -1;
12+
for (int i = 0; i < nums.length; i++) {
13+
if (isPrime(nums[i])) {
14+
min = i;
15+
break;
16+
}
17+
}
18+
int max = nums.length;
19+
for (int i = nums.length - 1; i >= min; i--) {
20+
if (isPrime(nums[i])) {
21+
max = i;
22+
break;
23+
}
24+
}
25+
return max - min;
26+
}
27+
28+
private boolean isPrime(int num) {
29+
if (num < 2) {
30+
return false;
31+
}
32+
if (num == 2) {
33+
return true;
34+
}
35+
if ((num & 1) == 0) {
36+
return false;
37+
}
38+
for (int i = 2; i * i <= num; i++) {
39+
if (num % i == 0) {
40+
return false;
41+
}
42+
}
43+
return true;
44+
}
45+
// end::answer[]
46+
}

0 commit comments

Comments
 (0)