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

Commita7aaf47

Browse files
fishercoder1534zhahui
authored andcommitted
add 1207
1 parent1738e27 commita7aaf47

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Your ideas/fixes/algorithms are more than welcome!
2727

2828
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2929
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
30+
|1207|[Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1207.java)| O(n)| O(1)||Easy||
3031
|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)| O(n)| O(m)||Easy||
3132
|1154|[Day of the Year](https://leetcode.com/problems/day-of-the-year/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1154.java)| O(1)| O(1)||Easy||
3233
|1137|[N-th Tribonacci Number](https://leetcode.com/problems/n-th-tribonacci-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1137.java)| O(n)| O(n)||Easy||
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.Arrays;
4+
importjava.util.HashMap;
5+
importjava.util.HashSet;
6+
importjava.util.Map;
7+
importjava.util.Set;
8+
9+
/**
10+
* 1207. Unique Number of Occurrences
11+
*
12+
* Given an array of integers arr,
13+
* write a function that returns true if and only if the number of occurrences of each value in the array is unique.
14+
*
15+
* Example 1:
16+
* Input: arr = [1,2,2,1,1,3]
17+
* Output: true
18+
* Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.
19+
*
20+
* Example 2:
21+
* Input: arr = [1,2]
22+
* Output: false
23+
*
24+
* Example 3:
25+
* Input: arr = [-3,0,1,-3,1,1,1,-3,10,0]
26+
* Output: true
27+
*
28+
* Constraints:
29+
* 1 <= arr.length <= 1000
30+
* -1000 <= arr[i] <= 1000
31+
* */
32+
publicclass_1207 {
33+
publicstaticclassSolution1 {
34+
publicbooleanuniqueOccurrences(int[]arr) {
35+
Map<Integer,Integer>map =newHashMap<>();
36+
Arrays.stream(arr).forEach(num -> {
37+
map.put(num,map.containsKey(num) ?map.get(num) +1 :1);
38+
});
39+
Set<Integer>set =newHashSet<>();
40+
returnmap.keySet().stream().mapToInt(key ->key).allMatch(key ->set.add(map.get(key)));
41+
}
42+
}
43+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1207;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_1207Test {
10+
privatestatic_1207.Solution1solution1;
11+
privatestaticint[]arr;
12+
13+
@BeforeClass
14+
publicstaticvoidsetup() {
15+
solution1 =new_1207.Solution1();
16+
}
17+
18+
@Test
19+
publicvoidtest1() {
20+
arr =newint[]{1,2,2,1,1,3};
21+
assertEquals(true,solution1.uniqueOccurrences(arr));
22+
}
23+
24+
@Test
25+
publicvoidtest2() {
26+
arr =newint[]{1,2};
27+
assertEquals(false,solution1.uniqueOccurrences(arr));
28+
}
29+
30+
@Test
31+
publicvoidtest3() {
32+
arr =newint[]{-3,0,1, -3,1,1,1, -3,10,0};
33+
assertEquals(true,solution1.uniqueOccurrences(arr));
34+
}
35+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp