adding set leetcode problem
This commit is contained in:
@@ -65,8 +65,9 @@ map_vect :: const_iterator p;
|
||||
|
||||
The compiler makes the substitution for you.
|
||||
|
||||
## 16.10 Leetcode Exercises
|
||||
## 16.3 Leetcode Exercises
|
||||
|
||||
- [Leetcode problem 49: Group Anagrams](https://leetcode.com/problems/group-anagrams/). Solution: [p49_group_anagrams.cpp](../../leetcode/p49_group_anagrams.cpp).
|
||||
- [Leetcode problem 290: Word Pattern](https://leetcode.com/problems/word-pattern/). Solution: [p290_word_pattern.cpp](../../leetcode/p290_word_pattern.cpp).
|
||||
- [Leetcode problem 1207: Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/). Solution: [p1207_unique_number_occurrences.cpp](../../leetcode/p1207_unique_number_occurrences.cpp).
|
||||
|
||||
|
||||
26
leetcode/p1207_unique_number_occurrence.cpp
Normal file
26
leetcode/p1207_unique_number_occurrence.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
class Solution {
|
||||
public:
|
||||
bool uniqueOccurrences(vector<int>& arr) {
|
||||
int size = arr.size();
|
||||
// key: number, value: occurrence
|
||||
std::map<int, int> map1;
|
||||
std::set<int> set1;
|
||||
// create the map
|
||||
for(int i=0; i<size; i++){
|
||||
map1[arr[i]]++;
|
||||
}
|
||||
// use the map
|
||||
std::map<int, int>::iterator itr;
|
||||
itr = map1.begin();
|
||||
while(itr != map1.end()){
|
||||
// if this number of occurrence already exists
|
||||
if(set1.find(itr->second) != set1.end()){
|
||||
return false;
|
||||
}
|
||||
// otherwise, store it.
|
||||
set1.insert(itr->second);
|
||||
itr++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user