leetcode 143. 重排链表 1461. 检查一个字符串是否包含所有长度为 K 的二进制子串 面试题 16.15. 珠玑妙算

it2023-02-09  43

143. 重排链表

# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def reorderList(self, head: ListNode) -> None: """ Do not return anything, modify head in-place instead. """ fakehead = ListNode(None,head) fast = fakehead slow = fakehead while fast and fast.next: fast = fast.next.next slow = slow.next even = True if fast else False # 此时 slow为中点,把之后的都倒置 print('here') left = slow.next if slow else None middle=left.next if left else None right = middle.next if middle else None while middle: left.next = right middle.next = slow.next slow.next = middle middle = right right = right.next if right else None left=head slow = slow.next if slow else None while slow: if left: print(left.val) fakehead.next = left fakehead = fakehead.next left = left.next if not slow: break if slow: print(slow.val) fakehead.next = slow slow = slow.next fakehead = fakehead.next if not even: fakehead.next = left fakehead = fakehead.next fakehead.next = None

1461. 检查一个字符串是否包含所有长度为 K 的二进制子串

class Solution: def hasAllCodes(self, s: str, k: int) -> bool: top_limit = int(pow(2,k)) if len(s)<int(pow(2,k)): return False visited = [False]*top_limit for i in range(k,len(s)+1): visited[int(s[i-k:i],2)] = True # print(visited) for v in visited: if not v: return False return True

面试题 16.15. 珠玑妙算

class Solution: def masterMind(self, solution: str, guess: str) -> List[int]: color_count = [0]*4 guess_count = [0]*4 bingo = 0 for i in range(len(solution)): c = solution[i] g = guess[i] if c==g: bingo+=1 else: color_count[0 if c=='R' else 1 if c=='Y' else 2 if c=='G' else 3]+=1 guess_count[0 if g=='R' else 1 if g=='Y' else 2 if g=='G' else 3]+=1 return [bingo,sum(list(map(lambda x,y:min(x,y),color_count,guess_count)))]
最新回复(0)