This is the first Ogawa 419 update, the first 452 Pian original
Look problems and prepare
Introduced today is LeetCode algorithm in title Easy level of 269 questions (overall title number is 1207 ). Given an array of integers arr
, if and only if the unique number of occurrences of each element in the array, return true
.
E.g:
Input: arr = [1,2,2,1,1,3]
Output: true
Description: The value 1 appears 3 times, 2 times the value 2 occurs, the value of 1 appears 3 times. No two values are the same number of occurrences.
Input: arr = [1,2]
Output: false
Input: arr = [-3,0,1, -3,1,1,1, -3,10,0]
Output: true
Restrictions :
- 1 <= arr.length <= 1000
- -1000 <= arr[i] <= 1000
The first solution
The title means that the number of occurrences of elements in the array to determine whether or not the only, we can directly use HashMap
and HashSet
combination of the first traversing arr
element, the element value as key
, the number of occurrences as a value
deposit HashMap
, and then traverse HashMap
all value
, deposit HashSet
, if The current value
name exists, return false
.
public boolean uniqueOccurrences(int[] arr) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int num : arr) {
map.put(num, map.getOrDefault(num, 0)+1);
}
Set<Integer> set = new HashSet<Integer>();
for (Integer count : map.values()) {
if (set.contains(count)) {
return false;
}
set.add(count);
}
return true;
}
The second solution
Use int
Array count. Because the subject and defines the length of the array element value range of the original, so we can resolve this problem by means of counting.
First count, a length of 2001 declare int
arrays count
, plus the value of the original array 1000 as a new index array. A second count, the same length as a declaration of the 2001 int
array count2
, if count
the current element is not equal to 0, as will the current element count2
array index value accumulation element, determines whether the addition was complete the element value is greater than or equal to 2, if it exceeds, direct return false
.
public boolean uniqueOccurrences2(int[] arr) {
int[] count = new int[2001];
for (int num : arr) {
count[num+1000]++;
}
int[] count2 = new int[2001];
for (int con : count) {
if (con != 0) {
count2[con]++;
if (count2[con] >= 2) {
return false;
}
}
}
return true;
}
A third solution
And a second solution similar to the above, except that the second step count int
array into a boolean
type, other thinking the same.
public boolean uniqueOccurrences3(int[] arr) {
int[] count = new int[2001];
for (int num : arr) {
count[num+1000]++;
}
boolean[] flag = new boolean[2001];
for (int con : count) {
if (con > 0) {
if (flag[con]) {
return false;
} else {
flag[con] = true;
}
}
}
return true;
}
summary
Thematic algorithm has now updated LeetCode algorithm of feature articles 275 + articles, public Number dialog box reply [ data structures and algorithms ], [ algorithm ], [ data structures Any words] in obtaining a series of articles collection.
That's all, if you have any good solution ideas, suggestions or other issues, you can exchange comments below, thumbs up, leave a message, it is the greatest reward in watching and supporting me!