一刷leetcode——图论

207. Course Schedule

题意:每个课程会有一个先修课程,给定一张图,判断能否按顺序修完所有课程

我的思路:拓扑排序裸题

我的代码:

class Solution {
public:
    struct Node {
        int to, next;
    };
    bool canFinish(int numCourses, vectorint, int>>& prerequisites) {
        if (prerequisites.size() == 0) return 1;
        Node edge[prerequisites.size()];    
        int head[numCourses], que[numCourses], iq = 0, indegree[numCourses];
        memset(head, -1, sizeof(head));
        memset(indegree, 0, sizeof(indegree));
        for (int i = 0; i < prerequisites.size(); i++) {
            edge[i].to = prerequisites[i].first;
            indegree[prerequisites[i].first]++;
            edge[i].next = head[prerequisites[i].second];
            head[prerequisites[i].second] = i;
        }
        for (int i = 0; i < numCourses; i++)
            if (indegree[i] == 0)
                que[iq++] = i;
        for (int i = 0; i < iq; i++) {
            for (int k = head[que[i]]; k != -1; k = edge[k].next) {
                indegree[edge[k].to]--;
                if (indegree[edge[k].to] == 0)
                    que[iq++] = edge[k].to;
            }
        }
        for (int i = 0; i < numCourses; i++)
            if (indegree[i] != 0) return 0;
        return 1;
    }
};
View Code

201.Course Schedule II

题意:将上题的课程序列输出

我的思路:拓扑排序

我的代码:

class Solution {
public:
    struct Node {
        int to, next;
    };
    vector<int> findOrder(int numCourses, vectorint, int>>& prerequisites) {
        vector<int> g, que;
        if (numCourses == 0) return g;
        Node edge[prerequisites.size()+1]; 
        int head[numCourses], iq = 0, indegree[numCourses];
        memset(head, -1, sizeof(head));
        memset(indegree, 0, sizeof(indegree));
        for (int i = 0; i < prerequisites.size(); i++) {
            edge[i].to = prerequisites[i].first;
            indegree[prerequisites[i].first]++;
            edge[i].next = head[prerequisites[i].second];
            head[prerequisites[i].second] = i;
        }
        for (int i = 0; i < numCourses; i++)
            if (indegree[i] == 0)
                que.push_back(i), iq++;
        for (int i = 0; i < iq; i++) {
            for (int k = head[que[i]]; k != -1; k = edge[k].next) {
                indegree[edge[k].to]--;
                if (indegree[edge[k].to] == 0)
                    que.push_back(edge[k].to), iq++;
            }
        }
        for (int i = 0; i < numCourses; i++)
            if (indegree[i] != 0) return g;
        return que;
    }
};
View Code

238. Product of Array Except Self

题意:给定一个数组,输出一个数组,它的每个元素是原数组除该位置以外其他所有元素的乘积

我的思路:开两个数组分别记录i位置左边元素的乘积和右边元素的乘积,结果即为两者相乘

我的代码:

class Solution {
public:
    vector<int> productExceptSelf(vector<int>& nums) {
        int n = nums.size();
        vector<int> fl(n, 1), fr(n, 1);
        for (int i = 1; i < n; i++) {
            fl[i] = fl[i-1]*nums[i-1];
            fr[n-1-i] = fr[n-i]*nums[n-i];
        }
        for (int i = 0; i < n; i++) fl[i] *= fr[i];
        return fl;
    }
};
View Code

743. Network Delay Time

题意:给定有向图和起点,输出走到全图任一点的最大代价 

我的思路:单源最短路Dijkstra

我的代码:

class Solution {
public:
    int networkDelayTime(vectorint>>& times, int N, int K) {
        vector<int> dist(N+1), flag(N+1, 0);
        vectorint>> mapp(N+1, vector<int>(N+1, 0x7fffffff));
        for (int i = 0; i < times.size(); i++)
            mapp[times[i][0]][times[i][1]] = times[i][2];
        for (int i = 1; i <= N; i++)
            if (i != K) dist[i] = mapp[K][i];
        dist[K] = 0;
        flag[K] = 1;
        int ans = 0;
        for (int i = 1; i < N; i++) {
            int minn = 0x7fffffff;
            int k = 0;
            for (int j = 1; j <= N; j++)
                if (!flag[j] && dist[j] < minn) {
                    k = j;
                    minn = dist[j];
                }
            if (k == 0) break;
            flag[k] = 1;
            for (int j = 1; j <= N; j++)
                if (!flag[j] && mapp[k][j] != 0x7fffffff && dist[j] > dist[k]+mapp[k][j])
                    dist[j] = dist[k]+mapp[k][j];
        }
        for (int i = 1; i <= N; i++)
            if (i != K) ans = max(ans, dist[i]);
        return ans == 0x7fffffff ? -1 : ans;
    }
};
View Code

 

转载于:https://www.cnblogs.com/hqxue/p/6921532.html

你可能感兴趣的:(java,数据结构与算法)