| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
此题就是求两个单链表的第一个交点int lca(int a, int b, int parent[])
{
int ha = 0, hb = 0, x = a, y = b;
while (parent[a]) ++ha, a = parent[a];
while (parent[b]) ++hb, b = parent[b];
while (ha > hb) x = parent[x], --ha;
while (hb > ha) y = parent[y], --hb;
while (x != y) x = parent[x], y = parent[y];
return x;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator