力扣剑指offer第39天图

114)外星人字典

class Solution{
public:
    int startsWith(string s, string sub){
        return s.find(sub) == 0 ? 1 : 0;
    }
    string alienOrder(vector<string>& words){
        unordered_map<char, unordered_set<char>> graph;
        unordered_map<char, int> inDegree;
        string res;
        for(int i=0; i<words.size(); i++){
            for(char ch : words[i]){
                graph[ch] = {};
                inDegree[ch] = 0;
            }
        }
        
        for(int i=0; i<words.size()-1; i++){
            string first=words[i], second=words[i+1];
            if(startsWith(first, second) && first.size()>second.size()) return "";
            for(int j=0; j<first.size() && j<second.size(); j++){
                char ch1=first[j], ch2=second[j];
                if(ch1==ch2) continue;
                else{
                    if(!graph[ch1].count(ch2)){
                        graph[ch1].insert(ch2);
                        inDegree[ch2]++;
                    }
                    break;
                }
            }
        }
        queue<char> que;
        for(auto ch : inDegree){
            if(ch.second == 0){
                que.push(ch.first);
            }
        }
        
        while(!que.empty()){
            char ch = que.front(); que.pop();
            res += ch;
            for(char next : graph[ch]){
                inDegree[next]--;
                if(inDegree[next]==0){
                    que.push(next);
                }
            }    
        }
        return res.size()==inDegree.size() ? res : "";
    }
};

115)重建序列

class Solution{
public:
    bool sequenceReconstruction(vector<int>& org, vector<vector<int>>& seqs){
        int n = org.size(), m=seqs.size(), index=0;
        if(m==0) return false;
        vector<int> inDegree(n+1);
        map<int, set<int>> adj;
        unordered_set<int> numSet;
        for(auto seq : seqs){
            int k = seq.size();
            if(seq[k-1]<1 || seq[k-1]>n) return false;
            numSet.insert(seq[k-1]);
            for(int i=0; i<k-1; i++){
                if(seq[i]<1 || seq[i]>n) return false;
                numSet.insert(seq[i]);
                if(!adj[seq[i]].count(seq[i+1])) inDegree[seq[i+1]]++;
                adj[seq[i]].insert(seq[i+1]);
            }
        }
        if(numSet.size()!=n) return false;
        queue<int> q;
        for(int i=1; i<=n ;i++){
            if(inDegree[i]==0) q.push(i);
        }
        if(q.size()!=1) return false;
        while(!q.empty()){
            if(q.size()!=1) return false;
            int cur = q.front(); q.pop();
            if(cur != org[index++]) return false;
            for(auto& next : adj[cur]){
                inDegree[next]--;
                if(inDegree[next]==0){
                    q.push(next);
                }
            }
        }
        return index==n;
    }
};

116)省份数量

深度优先遍历

class Solution{
public:
    int findCircleNum(vector<vector<int>>& isConnected){
        int provinces = isConnected.size();
        vector<int> visited(provinces);
        int circles = 0;
        for(int i=0; i<provinces; i++){
            if(!visited[i]){
                visited[i] = 1;
                dfs(isConnected, visited, provinces, i);
                circles++;
            }
        }
        return circles;
    }
    
    void dfs(vector<vector<int>>& isConnected, vector<int>& visited, int provinces, int j){
        for(int i=0; i<provinces; i++){
            if(isConnected[j][i] && !visited[i]){
                visited[i] = 1;
                dfs(isConnected, visited, provinces, i);
            }
        }
    }
};

广度优先遍历

class Solution{
public:
    int findCircleNum(vector<vector<int>>& isConnected){
        int provinces = isConnected.size();
        vector<int> visited(provinces);
        int circles = 0;
        
        queue<int> q;
        for(int i=0; i<provinces; i++){
            if(!visited[i]){
                q.push(i);
                while(!q.empty()){
                    int i = q.front(); q.pop();
                    visited[i] = 1;
                    for(int j=0; j<provinces; j++)
                        if(isConnected[i][j] && !visited[j]){
                            q.push(j);
                        }
                }
                circles++;
            }
        }
        return circles;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值