-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1198.cpp
More file actions
60 lines (50 loc) · 1.17 KB
/
Copy path1198.cpp
File metadata and controls
60 lines (50 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Solution {
public:
struct node
{
int v;
int s;
int cur;
};
//成员函数不能是私有的
struct cmp
{
bool operator() (struct node n1, struct node n2)
{
return n1.v > n2.v ;
};
};
int smallestCommonElement(vector<vector<int>>& mat) {
int m = mat.size();
int n = mat[0].size();
int cur[500];
int cnt[10001];
for(int i = 0; i <= 10000; ++i)
cnt[i] = 0;
for(int i = 0; i < m; ++i)
{
node n;
n.v = mat[i][0];
n.cur = 0;
n.s = i;
q.push(n);
if(++cnt[n.v] == m)return n.v;
}
//有一个数组耗光了就说明查找失败了
while(1)
{
node t = q.top();
q.pop();
cnt[t.v]--;
if(t.cur == n-1)return -1;
node p;
p.v = mat[t.s][t.cur+1];
p.cur = t.cur+1;
p.s = t.s;
q.push(p);
if(++cnt[p.v] == m)return p.v;
}
return -1;
}
priority_queue<node, vector<node>, cmp> q;
};