1. O(1) < O(logn) < O(n) < O(nlogn) < O(n2) < O(n3) < O(2n) < O(n!) < O(nn)
  2. 红黑树的操作时间跟二叉查找树的时间复杂度是一样的,执行查找、插入、删除等操作的时间复杂度为O(logn)

解法一:哈希表

  1. int longestConsecutive(vector<int>& nums) {
  2. unordered_set<int> numSet;
  3. for(int num : nums){
  4. numSet.insert(num);
  5. }
  6. int maxLen = 0, curNum = 0, curLen = 1;
  7. for(int num : numSet){
  8. if(numSet.count(num - 1))continue;
  9. curNum = num;
  10. curLen = 1;
  11. while(numSet.count(curNum + 1)){
  12. ++curLen;
  13. ++curNum;
  14. }
  15. maxLen = max(maxLen, curLen);
  16. }
  17. return maxLen;
  18. }