ÉèΪÊ×Ò³ ¼ÓÈëÊÕ²Ø

TOP

ÅжÏÁ½¸öµ¥Á´±íÊÇ·ñÏཻ
2014-11-24 02:23:09 ¡¾´ó ÖРС¡¿ ä¯ÀÀ:876´Î
Tags£ºÅÐ¶Ï Á½¸ö µ¥Á´±í ÊÇ·ñ Ïཻ

ÕâµÀÌâÓжàÖÖËã·¨¡£
Ëã·¨1£º°ÑµÚÒ»¸öÁ´±íÖðÏî´æÔÚhashtableÖУ¬±éÀúµÚ2¸öÁ´±íµÄÿһÏÈç¹ûÄÜÔÚµÚÒ»¸öÁ´±íÖÐÕÒµ½£¬Ôò±ØÈ»Ïཻ¡£
static bool JudgeIntersectLink1(Link head1, Link head2)
{
Hashtable ht = new Hashtable();
Link curr1 = head1;
Link curr2 = head2;
//store all the elements of link1
while (curr1.Next != null)
{
ht[curr1.Next] = string.Empty;
curr1 = curr1.Next;
}
//check all the elements in link2 if exists in Hashtable or not
while (curr2.Next != null)
{
//if exists
if (ht[curr2.Next] != null)
{
return true;
}
curr2 = curr2.Next;
}
return false;
}


Ëã·¨2£º°ÑÒ»¸öÁ´±íA½ÓÔÚÁíÒ»¸öÁ´±íBµÄĩ⣬Èç¹ûÓл·£¬Ôò±ØÈ»Ïཻ¡£ÈçºÎÅжÏÓл·ÄØ£¿´ÓA¿ªÊ¼±éÀú£¬Èç¹ûÄܻص½AµÄ±íÍ·£¬Ôò¿Ï¶¨Óл·¡£
×¢Ò⣬ÔÚ·µ»Ø½á¹û֮ǰ£¬Òª°Ñ¸Õ²ÅÁ¬½ÓÉϵÄÁ½¸öÁ´±í¶Ï¿ª£¬»Ö¸´Ô­×´¡£
static bool JudgeIntersectLink2(Link head1, Link head2)
{
bool exists = false;
Link curr1 = head1;
Link curr2 = head2;


//goto the end of the link1
while (curr1.Next != null)
{
curr1 = curr1.Next;
}
//join these two links
curr1.Next = head2;
//iterate link2
while (curr2.Next != null)
{
if (curr2.Next == head2)
{
exists = true;
break;
}
curr2 = curr2.Next;
}
//recover original status, whether exists or not
curr1.Next = null;
return exists;
}


Ëã·¨3£ºÈç¹ûÁ½¸öÁ´±íµÄĩβԪËØÏàͬ£¬Ôò±ØÏཻ¡£
static bool JudgeIntersectLink3(Link head1, Link head2)
{
Link curr1 = head1;
Link curr2 = head2;
//goto the end of the link1
while (curr1.Next != null)
{
curr1 = curr1.Next;
}
//goto the end of the link2
while (curr2.Next != null)
{
curr2 = curr2.Next;
}
if (curr1 != curr2)
return false;
else
return true;
}


¡¾´ó ÖРС¡¿¡¾´òÓ¡¡¿ ¡¾·±Ìå¡¿¡¾Í¶¸å¡¿¡¾Êղء¿ ¡¾ÍƼö¡¿¡¾¾Ù±¨¡¿¡¾ÆÀÂÛ¡¿ ¡¾¹Ø±Õ¡¿ ¡¾·µ»Ø¶¥²¿¡¿
ÉÏһƪ£ºOracle×óÁ¬½ÓºÍÓÒÁ¬½ÓʵÏÖ·½Ê½ ÏÂһƪ£º»ªÎªÃæÊÔÁ÷³Ì¡¢ÃæÊÔ¾­Ñé×ܽá

×îÐÂÎÄÕÂ

ÈÈÃÅÎÄÕÂ

Hot ÎÄÕÂ

Python

C ÓïÑÔ

C++»ù´¡

´óÊý¾Ý»ù´¡

linux±à³Ì»ù´¡

C/C++ÃæÊÔÌâÄ¿