这周题好难 老师也给了新的任务 感觉无从下手的样子 好难受 0.0
希望下周还有时间刷力扣
22:括号匹配 我知道用回溯 可是就是不会写 呜呜呜
class Solution(object): def generateParenthesis(self, n): """ :type n: int :rtype: List[str] """ rel = [] def DFS(l,r,currel): if l == n and r == n: rel.append(currel) return if l < n : currel += ‘(‘ DFS(l+1,r ,currel) # currel.pop() currel = currel[:-1] if r < l : currel += ‘)‘ DFS(l,r+1,currel) # currel.pop() currel = currel[:-1] DFS(0,0,‘‘) return rel 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/generate-parentheses/solution/hui-su-a-hui-su-hui-su-a-hui-su-by-yizhu-ik5q/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
23 : 合并k个链表 我第一反应是败者树 没想到2分合也可以
我提交的用的取巧法..
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution(object): def mergeKLists(self, lists): """ :type lists: List[ListNode] :rtype: ListNode """ listsnum = [0]*20001 for each in lists: while each!= None: listsnum[each.val +10000] += 1 each = each.next flag = 0 for i,j in enumerate(listsnum): if flag == 0: if j > 0: rel = ListNode(i-10000) rp = rel j -= 1 flag = 1 while j > 0: rp .next = ListNode(i-10000) rp = rp.next j -= 1 if flag == 0: return None else: return rel 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/merge-k-sorted-lists/solution/kule-by-yizhu-jia-ehms/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
24:
两两交换链表中的节点
这个问题在于边界问题 我用了很多if判断反正 没有想到加一个头结点的方法
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution(object): def swapPairs(self, head): """ :type head: ListNode :rtype: ListNode """ if head == None: return None q = head.next if q != None: head.next = q.next q.next = head s = head head = q #如果有两个节点以上 先交换他们 再用s指向交换的尾部 else: return head p = s.next if p == None: return head q = p.next while p != None and q != None: p.next = q.next s.next = q q.next = p s = p p = s.next if p == None: break q = p.next return head
25: 第一个自己做出来的困难题 而且效果好像还可以!!!!
翻转K个好像跟2个没区别
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution(object): def reverseKGroup(self, head, k): """ :type head: ListNode :type k: int :rtype: ListNode """ dummyhead = ListNode(0) dummyhead.next = head r = dummyhead traverpos = head while traverpos != None: num = 0 #用来计算有没有k个结点 for i in range(k): num += 1 traverpos = traverpos.next if traverpos == None: break if num == k: #读取到了k个结点 revpos = r.next p = revpos.next for i in range(k-1): tmp = p.next p.next = revpos revpos = p p = tmp tmp = r.next tmp.next = p r.next = revpos r = tmp return dummyhead.next 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/reverse-nodes-in-k-group/solution/ye-du-li-jie-jue-kun-nan-ti-ye-ye-ye-ye-xiit3/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
26 确实是简单题 等于一个选择插入排序
class Solution(object): def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ if len(nums) == 0: return 0 i = 0 for each in nums: if each != nums[i]: i += 1 nums[i] = each return i+1