-
Notifications
You must be signed in to change notification settings - Fork 1
/
1207.独一无二的出现次数.java
72 lines (70 loc) · 1.51 KB
/
1207.独一无二的出现次数.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* @lc app=leetcode.cn id=1207 lang=java
*
* [1207] 独一无二的出现次数
*
* https://leetcode-cn.com/problems/unique-number-of-occurrences/description/
*
* algorithms
* Easy (69.69%)
* Likes: 76
* Dislikes: 0
* Total Accepted: 32.3K
* Total Submissions: 43.8K
* Testcase Example: '[1,2,2,1,1,3]'
*
* 给你一个整数数组 arr,请你帮忙统计数组中每个数的出现次数。
*
* 如果每个数的出现次数都是独一无二的,就返回 true;否则返回 false。
*
*
*
* 示例 1:
*
* 输入:arr = [1,2,2,1,1,3]
* 输出:true
* 解释:在该数组中,1 出现了 3 次,2 出现了 2 次,3 只出现了 1 次。没有两个数的出现次数相同。
*
* 示例 2:
*
* 输入:arr = [1,2]
* 输出:false
*
*
* 示例 3:
*
* 输入:arr = [-3,0,1,-3,1,1,1,-3,10,0]
* 输出:true
*
*
*
*
* 提示:
*
*
* 1 <= arr.length <= 1000
* -1000 <= arr[i] <= 1000
*
*
*/
// @lc code=start
class Solution {
public boolean uniqueOccurrences(int[] arr) {
int[] count = new int[2002];
for (int i = 0; i < arr.length; i++) {
count[arr[i] + 1000]++;
}
int[] tmp = new int[1001];
for (int i = 0; i < count.length; i++) {
if (count[i] == 0) {
continue;
}
if (tmp[count[i]] == count[i]) {
return false;
}
tmp[count[i]] = count[i];
}
return true;
}
}
// @lc code=end