for index inrange(1,length-1): if pre_sum[index-1]==pre_sum[length-1]-pre_sum[index]: return index # 返回最左边下标,防止有x,x,x,x,..0,0,0,0的情况 if pre_sum[length-2]==0: return length-1 return -1
classSolution: defisSubsequence(self, s: str, t: str) -> bool: length=len(s) if length==0:returnTrue index=0 for item in t: if item==s[index]: index+=1 if index==length: returnTrue returnFalse
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next classSolution: defmergeTwoLists(self, a: Optional[ListNode], b: Optional[ListNode]) -> Optional[ListNode]: ifnot a:return b ifnot b:return a # 空表头 prehead=ListNode(0) pre=prehead while a and b: if a.val>b.val: pre.next=b b=b.next else: pre.next=a a=a.next pre=pre.next pre.next=a if a else b return prehead.next
classSolution: defdetectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: nums=dict() while head: if head in nums.keys(): return head else: nums[head]=1 head=head.next return head
classSolution: defmaxProfit(self, prices: List[int]) -> int: ans=0 profit=0 for index inrange(1,len(prices)): if prices[index]-prices[index-1]+profit<0: profit=0 else: profit+=prices[index]-prices[index-1] ans=max(ans,profit) return ans
classSolution: deflongestPalindrome(self, s: str) -> int: m=defaultdict(int) for item in s: m[item]+=1 ans=0 flag=0 for k,v in m.items(): if v&1: flag=1 v-=1 ans+=v return ans+flag
classSolution: defnumIslands(self, grid: List[List[str]]) -> int: n=len(grid) m=len(grid[0]) visited=[[0if grid[i][j]=='1'else1for j inrange(m)] for i inrange(n)] ans=0 for i inrange(n): for j inrange(m): ifnot visited[i][j]: ans+=1 dq=deque() dq.append((i,j)) while dq: x,y=dq.popleft() visited[x][y]=1 for x_,y_ in [(0,1),(0,-1),(-1,0),(1,0)]: if x_+x>=0and x_+x<n and y_+y>=0and y_+y<m: ifnot visited[x_+x][y_+y]: visited[x_+x][y_+y]=1 dq.append((x_+x,y_+y)) return ans
classSolution: defuniquePaths(self, m: int, n: int) -> int: a=[1for j inrange(n) ] b=[1for j inrange(n) ] for i inrange(1,m): for j inrange(1,n): b[j]=a[j]+b[j-1] a=b[:] return b[-1]
# dp=[[0 for j in range(n) ] for i in range(m)] # for i in range(m): # for j in range(n): # if i==0 or j==0: # dp[i][j]=1 # else: # dp[i][j]=dp[i][j-1]+dp[i-1][j] # return dp[m-1][n-1]
# dq=deque([(0,0)]) # ans=0 # while dq: # x,y=dq.pop() # if x==m-1 and y==n-1: # ans+=1 # for x_,y_ in [(x+1,y),(x,y+1)]: # if x_>=0 and x_<m and y_>=0 and y_<n: # dq.append((x_,y_)) # return ans
classSolution: deftwoSum(self, nums: List[int], target: int) -> List[int]: # nums=[(i,n) for i,n in enumerate(nums)] # nums=sorted(nums,key=lambda x:x[1]) # length=len(nums) # for i in range(length): # ttt=target-nums[i][1] # index=bisect_left(nums,ttt,key=lambda x:x[1]) # if index==i:continue # if index<0 or index>=length:continue # if nums[index][1]==ttt: # return [nums[i][0],nums[index][0]] hashtable = dict() for i, num inenumerate(nums): if target - num in hashtable: return [hashtable[target - num], i] hashtable[nums[i]] = i return []