c++ STL
set容器
insert后的set是从小到大排序的。
set.begin() < .. < set.rbegin()
参考代码:
1 class Solution { 2 public: 3 int thirdMax(vector<int>& nums) { 4 set<int> MaxSet; 5 for(int i=0; i<nums.size(); i++){ 6 MaxSet.insert(nums[i]); 7 if(MaxSet.size()>3) MaxSet.erase(MaxSet.begin()); 8 } 9 if(MaxSet.size()==3) return *MaxSet.begin(); 10 else return *MaxSet.rbegin(); 11 } 12 };