本文主要是介绍Week7-LeetCode,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
2923.找到冠军(简单)
法1:
class Solution:def findChampion(self, grid: List[List[int]]) -> int:Winner = 0n = len(grid)loser = [0 for _ in range(n)] for i in range(n):for j in range(n):if grid[i][j] == 1 and i != j:loser[j] = 1for index in range(n):if loser[index] == 0:return index
法2:
class Solution:def findChampion(self, grid: List[List[int]]) -> int:n = len(grid)for i, line in enumerate(grid):if sum(line) == n - 1:return i
22.括号生成(中等)
思路1→暴力法:
把所有可能的组合列出,去除不合格的可能。
class Solution:def generateParenthesis(self, n: int) -> List[str]:def generate(A):if len(A) == 2 * n:if valid(A):ans.append("".join(A))else:A.append('(')generate(A)A.pop()A.append(')')generate(A)A.pop()def valid(A):bal = 0for c in A:if c =='(':bal += 1else: bal -= 1if bal < 0:return Falsereturn bal == 0ans = []generate([])return ans
思路2→回溯法:
对思路1
进行改进,我们可以只在序列仍然保持有效时才添加 ‘(’
或 ‘)’
,而不是每次添加。我们可以通过跟踪到目前为止放置的左括号和右括号的数目来做到这一点。
class Solution:def generateParenthesis(self, n: int) -> List[str]:ans = []def backtrack(S, left, right):if len(S) == 2 * n:ans.append(''.join(S))return if left < n:S.append('(')backtrack(S, left + 1, right)S.pop()if right < left:S.append(')')backtrack(S, left, right + 1)S.pop()backtrack([], 0, 0)return ans
思路3→递归法:
class Solution:@lru_cache(None)def generateParenthesis(self, n: int) -> List[str]:if n == 0:return ['']ans = []for c in range(n):for left in self.generateParenthesis(c):for right in self.generateParenthesis(n - 1 - c):ans.append('({}){}'.format(left, right))return ans
23. 合并K个升序链表(困难)
思路:
把链表元素全部加到数组中,对数组排序,然后将数组元素组成链表。
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:nums = []head = ListNode(0)current = headfor li in lists:while li:nums.append(li.val)li = li.nextnums.sort()for num in nums:current.next = ListNode(num)current = current.nextreturn head.next
2924. 找到冠军 II (中等)
思路:
把所有节点的degree
值设为0
,意为没输过,如果遍历到一个节点没输过,且当前没有冠军节点,那么这个节点就是冠军,否则就是没有冠军。
class Solution:def findChampion(self, n: int, edges: List[List[int]]) -> int:degree = [0] * n for x, y in edges:degree[y] += 1champion = -1for i, d in enumerate(degree):if d == 0:if champion == -1:champion = ielse:return -1return champion
24.两两交换链表中的节点 (中等)
关键思路:
需要额外的节点来记录prev, cur, temp, next
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:if head == None or head.next == None:return headtemp = ListNode(0)next_one = ListNode(0)prev = ListNode(0)newhead = ListNode(0)newhead.next = headprev = newheadcur = headwhile cur != None and cur.next != None:next_one = cur.next.nexttemp = cur.nextcur.next = temp.nexttemp.next = curprev.next = tempprev = curcur = next_onereturn newhead.next
25. K 个一组翻转链表(困难)
关键思路:
分为两个函数去实现:
函数1
:把K
个链表全部倒转,需要四个辅助指针。
函数2
:找到K
个链表,把K
个链表取下再装回,需要四个辅助指针。
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:def reverse(self, head:ListNode, tail:ListNode):prev = tail.nextp = headwhile prev != tail:nex = p.nextp.next = prevprev = pp = nexreturn tail, head def reverseKGroup(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:hair = ListNode(0)hair.next = headpre = hairwhile head:tail = pre# 查看剩余部分长度是否大于等于 kfor i in range(k):tail = tail.nextif not tail:return hair.nextnex = tail.nexthead, tail = self.reverse(head, tail)# 把子链表重新接回原链表pre.next = headtail.next = nexpre = tailhead = tail.nextreturn hair.next
26.删除有序数组中的重复项(简单)
class Solution:def removeDuplicates(self, nums: List[int]) -> int:n = len(nums)index = 0for i in range(1, n):if nums[i] != nums[index]:index += 1nums[index] = nums[i]return index + 1
27. 移除元素 (简单)
class Solution:def removeElement(self, nums: List[int], val: int) -> int:index = 0for i in range(len(nums)):if nums[i] != val:nums[index] = nums[i]index += 1return index
28. 找出字符串中第一个匹配项的下标 (简单)
class Solution:def strStr(self, haystack: str, needle: str) -> int:h = len(haystack)n = len(needle)i = 0index = 0while i <= h - n:for j in range(n):if haystack[i + j] == needle[j]:index += 1if index == n:return iindex = 0i += 1return -1
29. 两数相除 (中等)
关键思路:
为方便运算,将除数和被除数的符号统一;又为了不溢出,故将除数和被除数变为负数。
同样,在运算过程中为了避免溢出,把 A+B < C
的比较换为 A < C - B
。
class Solution:def divide(self, dividend: int, divisor: int) -> int:INT_MIN, INT_MAX = -2**31, 2 ** 31 - 1if dividend == INT_MIN:if divisor == 1:return INT_MINif divisor == -1:return INT_MAXif divisor == INT_MIN:return 1 if dividend == INT_MIN else 0if dividend == 0:return 0rev = Falseif dividend > 0:dividend = -dividendrev = not revif divisor > 0 :divisor = -divisorrev = not revdef quickAdd(y:int, z:int, x:int) -> bool:result, add = 0, ywhile z > 0:if (z & 1) == 1:if result < x - add:return Falseresult += addif z != 1:if add < x - add:return Falseadd += add z >>= 1return Trueleft, right, ans = 1, INT_MAX, 0while left <= right:# 注意溢出,并且不能使用除法mid = left + ((right - left) >> 1)check = quickAdd(divisor, mid, dividend)if check:ans = mid# 注意溢出if mid == INT_MAX:breakleft = mid + 1else:right = mid - 1return -ans if rev else ans
这篇关于Week7-LeetCode的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!