zoukankan      html  css  js  c++  java
  • leetcode——1171. 从链表中删去总和值为零的连续节点

    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        def removeZeroSumSublists(self, head: ListNode) -> ListNode:
            r=[]
            while head:
                r.append(head.val)
                head=head.next
            #print(r)
            i=0
            while i<len(r):
                sum=r[i]
                j=i+1
                while j<len(r) and sum!=0:
                    sum+=r[j]
                    j+=1
                #print(i,j,sum)
                if sum==0:
                    r=r[:i]+r[j:]
                else:
                    i+=1
            head=ListNode(0)
            p=head
            for i in range(len(r)):
                p.next=ListNode(r[i])
                p=p.next
            return head.next
                    
    执行用时 :180 ms, 在所有 python3 提交中击败了32.63%的用户
    内存消耗 :13.9 MB, 在所有 python3 提交中击败了100.00%的用户
     
    执行用时为 48 ms 的范例
    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        def removeZeroSumSublists(self, head: ListNode) -> ListNode:
            handle = ListNode(0)
            handle.next = cur = head
            prefix = 0
            m = {0:handle}
            while cur:
                prefix += cur.val
                if prefix in m:
                    m[prefix].next = cur.next
                else:
                    m[prefix] = cur
                cur = cur.next
            return handle.next

    这个我还不能看得太懂,不会做。。。。

                                                       ——2019.10.24

     
    我的前方是万里征途,星辰大海!!
  • 相关阅读:
    (二十九)动态单元格
    (二十八)QQ好友列表的展开收缩
    (二十七)QQ好友列表的实现
    (二十六)静态单元格(Cell)
    (二十五)键盘的设置与TextField细节处理
    poj 1734 Sightseeing trip
    BZOJ 2200: [Usaco2011 Jan]道路和航线
    LUOGU P1073 最优贸易
    poj 3662 Telephone Lines
    poj 3539 Elevator
  • 原文地址:https://www.cnblogs.com/taoyuxin/p/11731394.html
Copyright © 2011-2022 走看看