# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None
classSolution(object): defdeleteDuplicates(self, head): """ :type head: ListNode :rtype: ListNode """ whilenot head ornot head.next: return head dummy = ListNode(-1) dummy.next = head slow = dummy fast = dummy.next while fast: if fast.nextand fast.val == fast.next.val: tmp = fast.val while fast and fast.val == tmp: fast = fast.next else: slow.next = fast slow = fast fast = fast.next slow.next = fast return dummy.next
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None
classSolution(object): defdeleteDuplicates(self, head): """ :type head: ListNode :rtype: ListNode """ whilenot head ornot head.next: return head dummy = cur = pre = ListNode(None) while head: while head and ((head.val == pre.val) or (head.nextand head.val == head.next.val)): pre = head head = head.next cur.next = head cur = cur.next if head: head = head.next return dummy.next
3. 删除排序链表中的重复项(Easy)
给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
示例 1:
1 2
输入: 1->1->2 输出: 1->2
示例 2:
1 2
输入: 1->1->2->3->3 输出: 1->2->3
解答:
思路:dummy大法
很简单啊,dummy大法在解决这种链表问题上,很有用。
1 2 3 4 5 6 7 8 9 10 11 12
classSolution(object): defdeleteDuplicates(self, head): """ :type head: ListNode :rtype: ListNode """ dummy = head while head: while head.nextand head.next.val == head.val: head.next = head.next.next# skip duplicated node head = head.next# not duplicate of current node, move to next node return dummy
classSolution(object): deflargestRectangleArea(self, heights): """ :type heights: List[int] :rtype: int """ res = 0 n = len(heights) for i inrange(n): l = i r = i while l >= 0and heights[l] >= heights[i]: l -= 1 while r < n and heights[r] >= heights[i]: r += 1 res = max(res,heights[i]*(r-l-1)) return res
暴力解法2:思路很简单,同样也超时
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution(object): deflargestRectangleArea(self, heights): """ :type heights: List[int] :rtype: int """ res = 0 n = len(heights) for i inrange(n): minHeight = float('inf') for j inrange(i,n): minHeight = min(minHeight,heights[j]) res = max(res,minHeight*(j-i+1)) return res
思路三:这个题是真的难。。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution(object): deflargestRectangleArea(self, heights): """ :type heights: List[int] :rtype: int """ stack = [-1] res = 0 n = len(heights) for i inrange(n): while stack[-1] != -1and heights[stack[-1]] >= heights[i]: tmp = stack.pop() res = max(res,heights[tmp] * (i-stack[-1]-1)) stack.append(i) while stack[-1] != -1: tmp = stack.pop() res = max(res,heights[tmp]*(n-stack[-1]-1)) return res
我个人觉得,下面这个解答,更容易理解。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution(object): deflargestRectangleArea(self, heights): """ :type heights: List[int] :rtype: int """ heights.append(0) area, stack = 0, [-1] for idx, height inenumerate(heights): while height < heights[stack[-1]]: h = heights[stack.pop()] w = idx - stack[-1] - 1 area = max(w*h, area) stack.append(idx) return area