Leetcode刷题 Day3-4 困得要死于是合并

昨天的 #20 有效的括号

因为困得半死挑了个简单的做了(三度打成左乐唉乐压抑)困得半死但是看风都侦探到凌晨四点半

菲利普太可爱了没办法不要用这种理由替自己开脱啊八嘎雅鹿

主要是做过比这个麻烦的然后就直接套了现成的代码

核心是栈

class Solution {
public:
   bool isMatch(char left, char right)
  {
       return (left == '(' && right == ')') ||
              (left == '[' && right == ']') ||
              (left == '{' && right == '}');
  }
   bool isValid(string s) {
       stack<char> stk;
       for (char ch : s)
      {
           if (ch == '(' || ch == '[' || ch == '{')
          {
               stk.push(ch);
          }
           else if (ch == ')' || ch == ']' || ch == '}')
          {
               if (stk.empty() || !isMatch(stk.top(), ch))
              {
                   return false;
              }
               stk.pop();
          }
      }
​
       return stk.empty() ? true : false;
  }
};

今天的 #128 最长连续序列

1.简单解:归并排序+遍历求解

那我能想到的最简单暴力的办法就是先排序再遍历嘛 顺手复习一下归并

class Solution {
public:
   void Merge(vector<int> &input,int start,int mid,int end)
  {
       vector<int> temp;
       int i=start,j=mid;
       while(i<mid&&j<end)
      {
           if(input[i]<=input[j])
          {
               temp.push_back(input[i++]);
          }
           else
          {
               temp.push_back(input[j++]);
          }
      }
       while(i<mid)
      {
           temp.push_back(input[i++]);
      }
       while(j<end)
      {
           temp.push_back(input[j++]);
      }
       for(int k=0;k<temp.size();k++)
      {
           input[start+k]=temp[k];
      }
  }
​
   void MergeSort(vector<int> &input,int start,int end)
  {
       int mid=(start+end)/2;
       if(end-start<=1)
      {
           return;
      }
       MergeSort(input,start,mid);
       MergeSort(input,mid,end);
       Merge(input,start,mid,end);
  }
   int longestConsecutive(vector<int>& nums) {
       if(nums.size()==0) return 0;
       MergeSort(nums,0,nums.size());
       int count=1;
       int precount=1;
       for(int i=1;i<nums.size();i++){
           if(nums[i]==nums[i-1]+1){
           count++;
          }
           else if(nums[i]==nums[i-1]){
               continue;}
           else{
               count=1;
          }
           if(count>precount){
                   precount=count;
          }
      }
       return precount;
  }
};

归并排序时间复杂度O(n log⁡ n)O(n \log n),遍历复杂度是O(n)O(n)

加起来是O(n log⁡ n)O(n \log n)

虽然过了但是其实超时了。。。。。

2.依旧哈希表

查了一下

感觉思路是

把数组里所有的值存进哈希表作为键

然后遍历数组的时候查当前数-1是否存在(是不是起点)

当前数+1是否存在(道路是否在延伸

最后计算最长的路线

class Solution {
public:
   int longestConsecutive(vector<int>& nums) {
       if(nums.size()==0) return 0;
       unordered_map<int,int> num;
       for(int i=0;i<nums.size();i++)
      {
           num[nums[i]]=nums[i];
      }
       int count=1,length=1;;
       for (const auto& pair : num) {
           if(num.find(pair.first-1)!=num.end())
          {
               continue;
          }
           else if(num.find(pair.first+1)!=num.end())
          {
               while(num.find(pair.first+count)!=num.end())
              {
                   count++;
              }
               if(count>length)
              {
                   length=count;
              }
               count=1;
          }
      }
       return length;
  }
};

时间复杂度是O(n)+O(n)=O(n)O(n)+O(n)=O(n)

3.似乎还有一个解法叫做并查集,可惜我不会

(或许以后会补充吧)

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇