我有3列ULS,每列都是一个动态UL容器,可以包含从0-9 Li容器到任何地方(最终更多)。我所有的Li元素都有一个属性“rel”,我试图找到该属性,并将其用于父DIV中所有Li元素的其他内容。我最终想要找到更多的基于每一个,但不是最起码的..。有什么想法吗?我怎样才能用jQuery实现这一点?例子:
<ul id="column1">
<li rel="1">Info</li>
<li rel="2">Info</li>
<li rel="3">Info</li>
</ul>
<ul id="column2">
<li rel="4">Info</li>
<li rel="5">Info</li>
<li rel="6">Info</li>
</ul>
<ul id="column3">
<li rel="7">Info</li>
<li rel="8">Info</li>
<li rel="9">Info</li>
</ul>
这些元素也是可以分类的。因此,当我得到一个列表,我也想保持他们的顺序,他们是从上到下的每一列。
你可以利用弗洛伊德的循环寻找算法,也被称为乌龟和兔子算法。
这个想法是有两个引用列表,并以不同的速度移动它们。一个1节点向前移动一个,另一个2节点移动。
如果链表有一个循环,他们一定会见面。
否则两个引用(或他们next)中的任何一个都会变成null。
实现该算法的Java函数:
boolean hasLoop(Node first) {
if(first == null) // list does not exist..so no loop eithe
return false;
Node slow, fast; // create two references.
slow = fast = first; // make both refer to the start of the list
while(true) {
slow = slow.next; // 1 hop
if(fast.next != null)
fast = fast.next.next; // 2 hops
else
return false; // next node null => no loop
if(slow == null || fast == null) // if either hits null..no loop
return false;
if(slow == fast) // if the two ever meet...we must have a loop
return true;
}
}
面是Fast / Slow解决方案的一个改进,它可以正确处理奇数长度的列表并提高清晰度。
boolean hasLoop(Node first) {
Node slow = first;
Node fast = first;
while(fast != null && fast.next != null) {
slow = slow.next; // 1 hop
fast = fast.next.next; // 2 hops
if(slow == fast) // fast caught up to slow, so there is a loop
return true;
}
return false; // fast reached null, so the list terminates
}