weihuaw
BAN USERAre you a c++ developer? Diamond inheritance can be resolved with virtual inheritance.
class Breather
{
virtual Breath();
}
class Walker: public virtual Breather
{
virtual Walk();
};
class Talker: public virtual Breather
{
virtual Talk();
};
class Swimmer: public Walker, Talker
{
virtual Swim();
};
C++ implementation of Ankit's way
insert o(lgN)
lookup o(1)
delete o(lgN)
class Meeting
{
public:
int start;
int end;
Meeting(){start = end = 0;};
Meeting(int s, int e):start(s),end(e){};
bool operator < (const Meeting& o) const
{
return this->end < o.start;
}
bool operator == (const Meeting& o) const
{
return !(*this < o || o < *this) ;
}
};
class MeetingManager
{
public:
MeetingManager(){};
bool AddMeeting(Meeting& m)
{
if(setMeeting.find(m) != setMeeting.end())
return false;
setMeeting.insert(m);
mapMeetings[MeetingToKey(m)] = m;
}
bool GetMeeting(int start, int end, Meeting& m)
{
string sKey = MeetingToKey(Meeting(start,end));
if(mapMeetings.find(sKey) == mapMeetings.end())
return false;
m = mapMeetings[sKey];
}
void DeleteMeeting(const Meeting& m)
{
string sKey = MeetingToKey(m);
if(mapMeetings.find(sKey) == mapMeetings.end())
return ;
mapMeetings.erase(sKey);
setMeeting.erase(m);
}
private:
set<Meeting> setMeeting;
unordered_map<string, Meeting> mapMeetings;
string MeetingToKey(const Meeting& m)
{
char buf[255];
sprintf_s(buf, "%d,%d", m.start, m.end);
string s(buf);
return s;
}
};
delete not good.
- weihuaw February 21, 2014