Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita9fd697

Browse files
add 1175
1 parentb59cb73 commita9fd697

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ _If you like this project, please leave me a star._ ★
235235
|1182|[Shortest Distance to Target Color](https://leetcode.com/problems/shortest-distance-to-target-color/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1182.java)||Medium|Binary Search|
236236
|1180|[Count Substrings with Only One Distinct Letter](https://leetcode.com/problems/count-substrings-with-only-one-distinct-letter/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1180.java)||Easy|Math, String|
237237
|1176|[Diet Plan Performance](https://leetcode.com/problems/diet-plan-performance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1176.java)||Easy|Array, Sliding Window|
238-
|1175|[Prime Arrangements](https://leetcode.com/problems/prime-arrangements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1175.java)||Easy||
238+
|1175|[Prime Arrangements](https://leetcode.com/problems/prime-arrangements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1175.java)||Easy|Math|
239239
|1165|[Single-Row Keyboard](https://leetcode.com/problems/single-row-keyboard/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1165.java)||Easy||
240240
|1161|[Maximum Level Sum of a Binary Tree](https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1161.java)||Medium|Graph|
241241
|1160|[Find Words That Can Be Formed by Characters](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1160.java)||Easy||
Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,46 @@
11
packagecom.fishercoder.solutions;
22

3+
importjava.math.BigInteger;
4+
importjava.util.Arrays;
5+
36
publicclass_1175 {
47
publicstaticclassSolution1 {
5-
publicintnumPrimeArrangements(intn) {
6-
//TODO: implement it
7-
return -1;
8+
//credit: https://leetcode.com/problems/prime-arrangements/discuss/371884/Simple-Java-With-comment-sieve_of_eratosthenes
9+
staticintMOD =1000000007;
10+
11+
publicstaticintnumPrimeArrangements(intn) {
12+
intnumberOfPrimes =generatePrimes(n);
13+
BigIntegerx =factorial(numberOfPrimes);
14+
BigIntegery =factorial(n -numberOfPrimes);
15+
returnx.multiply(y).mod(BigInteger.valueOf(MOD)).intValue();
16+
}
17+
18+
publicstaticBigIntegerfactorial(intn) {
19+
BigIntegerfac =BigInteger.ONE;
20+
for (inti =2;i <=n;i++) {
21+
fac =fac.multiply(BigInteger.valueOf(i));
22+
}
23+
returnfac.mod(BigInteger.valueOf(MOD));
24+
}
25+
26+
publicstaticintgeneratePrimes(intn) {
27+
boolean[]prime =newboolean[n +1];
28+
Arrays.fill(prime,2,n +1,true);
29+
for (inti =2;i *i <=n;i++) {
30+
if (prime[i]) {
31+
for (intj =i *i;j <=n;j +=i) {
32+
prime[j] =false;
33+
}
34+
}
35+
}
36+
intcount =0;
37+
for (inti =0;i <prime.length;i++) {
38+
if (prime[i]) {
39+
count++;
40+
}
41+
}
42+
43+
returncount;
844
}
945
}
1046
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp