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

Commit4054d41

Browse files
refactor 1394
1 parent1039b29 commit4054d41

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1394|[Find Lucky Integer in an Array](https://leetcode.com/problems/find-lucky-integer-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1394.java)||Easy|Array|
1112
|1392|[Longest Happy Prefix](https://leetcode.com/problems/longest-happy-prefix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1392.java)||Hard|String, Rolling Hash|
1213
|1390|[Four Divisors](https://leetcode.com/problems/four-divisors/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1390.java)||Medium|Math|
1314
|1389|[Create Target Array in the Given Order](https://leetcode.com/problems/create-target-array-in-the-given-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1389.java)||Easy|Array|
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.HashMap;
4+
importjava.util.Map;
5+
6+
/**
7+
* 1394. Find Lucky Integer in an Array
8+
*
9+
* Given an array of integers arr, a lucky integer is an integer which has a frequency in the array equal to its value.
10+
* Return a lucky integer in the array. If there are multiple lucky integers return the largest of them. If there is no lucky integer return -1.
11+
*
12+
* Example 1:
13+
* Input: arr = [2,2,3,4]
14+
* Output: 2
15+
* Explanation: The only lucky number in the array is 2 because frequency[2] == 2.
16+
*
17+
* Example 2:
18+
* Input: arr = [1,2,2,3,3,3]
19+
* Output: 3
20+
* Explanation: 1, 2 and 3 are all lucky numbers, return the largest of them.
21+
*
22+
* Example 3:
23+
* Input: arr = [2,2,2,3,3]
24+
* Output: -1
25+
* Explanation: There are no lucky numbers in the array.
26+
*
27+
* Example 4:
28+
* Input: arr = [5]
29+
* Output: -1
30+
*
31+
* Example 5:
32+
* Input: arr = [7,7,7,7,7,7,7]
33+
* Output: 7
34+
*
35+
* Constraints:
36+
* 1 <= arr.length <= 500
37+
* 1 <= arr[i] <= 500
38+
* */
39+
publicclass_1394 {
40+
publicstaticclassSolution1 {
41+
publicintfindLucky(int[]arr) {
42+
intlucky = -1;
43+
Map<Integer,Integer>map =newHashMap<>();
44+
for (inti =0;i <arr.length;i++) {
45+
map.put(arr[i],map.getOrDefault(arr[i],0) +1);
46+
}
47+
for (intnum :map.keySet()) {
48+
if (num ==map.get(num) &&num >lucky) {
49+
lucky =num;
50+
}
51+
}
52+
returnlucky;
53+
}
54+
}
55+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1394;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_1394Test {
10+
11+
privatestatic_1394.Solution1solution1;
12+
13+
@BeforeClass
14+
publicstaticvoidsetup() {
15+
solution1 =new_1394.Solution1();
16+
}
17+
18+
@Test
19+
publicvoidtest1() {
20+
assertEquals(2,solution1.findLucky(newint[]{2,2,3,4}));
21+
}
22+
23+
@Test
24+
publicvoidtest2() {
25+
assertEquals(3,solution1.findLucky(newint[]{1,2,2,3,3,3}));
26+
}
27+
28+
@Test
29+
publicvoidtest3() {
30+
assertEquals(-1,solution1.findLucky(newint[]{2,2,2,3,3}));
31+
}
32+
33+
@Test
34+
publicvoidtest4() {
35+
assertEquals(-1,solution1.findLucky(newint[]{5}));
36+
}
37+
38+
39+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp