adding anothe set problem

This commit is contained in:
Jidong Xiao
2023-10-24 13:22:31 -04:00
parent 557e40636a
commit 1bef6b5916
2 changed files with 41 additions and 3 deletions

View File

@@ -0,0 +1,29 @@
class Solution {
public:
int thirdMax(vector<int>& nums) {
std::set<int> set1;
int size = nums.size();
// create the set, the difference between set1 and nums is that elements in set1 is unique and is sorted.
for(int i=0;i<size;i++){
set1.insert(nums[i]);
}
std::set<int>::iterator itr1 = set1.end();
int result;
itr1--;
result = *itr1; // store the max number
// if 3rd max doesn't exist
if(itr1==set1.begin()){
return result;
}
itr1--;
// still, if 3rd max doesn't exist
if(itr1==set1.begin()){
return result;
}
itr1--;
// get the 3rd max
result = *itr1;
return result;
}
};