86
Title description:
Give you the head node head of a linked list and a specific value x. Please separate the linked list so that all nodes less than x appear before nodes greater than or equal to x.
You should keep the initial relative position of each node in the two partitions.
Example:
Answer:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def partition(self, head: ListNode, x: int) -> ListNode:
mins = ListNode(100)
maxs = ListNode(99)
minb = mins
maxb = maxs
while(head):
if head.val < x:
mins.next = head
mins = mins.next
else:
maxs.next = head
maxs = maxs.next
head = head.next
#print mins.val,maxs.val
if minb.next == None:
return maxb.next
elif maxb.next == None:
return minb.next
else:
maxs.next = None
mins.next = maxb.next
return minb.next
141
Title description:
Given a linked list, determine whether there are loops in the linked list.
If there is a node in the linked list that can be reached again by continuously tracking the next pointer, there is a ring in the linked list. In order to represent the rings in a given linked list, we use the integer pos to indicate the position where the end of the linked list is connected to the linked list (the index starts from 0). If pos is -1, then there is no ring in the linked list. Note: pos is not passed as a parameter, just to identify the actual situation of the linked list.
If there is a ring in the linked list, return true. Otherwise, it returns false.
Example:
Answer:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def hasCycle(self, head: ListNode) -> bool:
if not head:
return False
while head.next and head.val!=None:
head.val=None #遍历过的值置为空
head=head.next
if not head.next: #如果碰到空发现已经结束,则无环
return False
return True
287
Title description:
Given an array nums containing n + 1 integers, whose numbers are between 1 and n (including 1 and n), it can be seen that there is at least one repeated integer.
Assuming that nums has only one repeated integer, find the repeated number.
Example:
answer:
class Solution:
def findDuplicate(self, nums: List[int]) -> int:
left=1
right=len(nums)
while left<right:
mid=int(left+(right-left)/2)
cnt=0
for num in nums:
if num<=mid:
cnt+=1
if cnt<=mid:
left=mid+1
else:
right=mid
return right
844
Title description:
Given two strings of S and T, when they are input into a blank text editor respectively, judge whether the two are equal, and return the result. # Represents the backspace character.
Note: If you enter a backspace character for empty text, the text will remain empty.
Example:
Answer:
class Solution:
def backspaceCompare(self, S: str, T: str) -> bool:
a=[]
b=[]
for i in range(len(S)):
if S[i]!='#':
a.append(S[i])
elif S[i]=='#' and a!=[]:
a.pop()
for i in range(len(T)):
if T[i]!='#':
b.append(T[i])
elif T[i]=='#' and b!=[]:
b.pop()
return a==b
1438
Title description:
Give you an integer array nums and an integer limit representing the limit. Please return the length of the longest continuous sub-array. The absolute difference between any two elements in the sub-array must be less than or equal to limit.
If there is no sub-array that meets the condition, 0 is returned.
Example:
Answer:
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
s_min = deque() #单调栈,栈首是当前窗口最小值
s_max = deque() #栈首是当前窗口最大值
length = l = r = 0
while r < len(nums):
if s_max and s_min and s_max[0]-s_min[0] > limit:
if nums[l] == s_max[0]:
s_max.popleft()
if nums[l] == s_min[0]:
s_min.popleft()
l += 1
else:
# 更新当前区间最小值
while s_min and s_min[-1] > nums[r]:
s_min.pop()
s_min.append(nums[r])
# 更新当前区间最大值
while s_max and s_max[-1] < nums[r]:
s_max.pop()
s_max.append(nums[r])
r += 1
if s_max[0]-s_min[0] <= limit:
length = max(length, r-l)
return length