遍历整个链表 然后判断 每个节点是否为临界点 ,之后将位置放入列表中,同时记录最近距离,最后返回列表首尾差值即为最大距离
class Solution:
def nodesBetweenCriticalPoints(self, head: Optional[ListNode]) -> List[int]:
res1,res2 =-1,-1
dislist = []
node = head
nodenex =head.next
num = 1
while nodenex:
if nodenex.next and ((node.val<nodenex.val and nodenex.val>nodenex.next.val) or (node.val>nodenex.val and nodenex.val<nodenex.next.val)) :
if dislist:
if res1 ==-1:
res1 =num-dislist[-1]
else :
res1 =min(res1,num-dislist[-1])
dislist.append(num)
num += 1
node =nodenex
nodenex =nodenex.next
if len(dislist)>=2:
res2 = dislist[-1]-dislist[0]
return [res1,res2]
字符串题 直接判断是否单词是回文
class Solution:
def firstPalindrome(self, words: List[str]) -> str:
for word in words:
if word == word[::-1]:
return word
return ""
|