leeetcode1171 Remove Zero Sum Consecutive Nodes from Linked List
"""
Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences. After doing so, return the head of the final linked list. You may return any such answer. (Note that in the examples below, all sequences are serializations of ListNode objects.) Example 1: Input: head = [1,2,-3,3,1]
Output: [3,1]
Note: The answer [1,2,1] would also be accepted. Example 2: Input: head = [1,2,3,-3,4]
Output: [1,2,4] Example 3: Input: head = [1,2,3,-3,-2]
Output: [1] """
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None class Solution1(object):
def removeZeroSumSublists(self, head):
"""
:param head: ListNode
:return: ListNode
"""
if not head.next:
return head if head.val != 0 else None #判断头节点是为否为空
list = [] #建立list存储链表转化后的数组
p = head #建立p指针指向头结点
while(p): #将链表转为数组
list.append(p.val)
p = p.next
list = self.remove(list) #!!!删除连续和为0
newhead = ListNode(-1) #建立新的头结点
p = newhead #p指向新的头结点
for num in list: #将结果数组转成链表
p.next = ListNode(num)
p = p.next
return newhead.next
"""
在一个数组里把连续和为0的部分删除,两层循环:用i对每个元素遍历
再用j不断的对当前子数组求和,若为0,删除当前部分并进行递归
"""
def remove(self, list): #在一个数组里把连续和为0的部分删除
for i in range(len(list)):
sum = list[i]
j = i + 1
while(j <= len(list)):
if sum == 0:
return self.remove(list[:i] + list[j:]) #递归处理
else:
if j == len(list):
break
sum += list[j]
j += 1
return list """
用一个变量pre_sum记录前缀和,再用一个哈希表记录出现过的前缀和,
如果出现了两个相同的前缀和,就说明中间这一段的和为0,是多余的。
举例:
对于输入 [1, 2, -2, 3, -1, -1, -1],
前缀和为[1, 3, 1, 4, 3, 2, 1],
下标为0的1和下标为2的1相同,
就代表下标在【1, 2】间的元素的和为0。
"""
class Solution2(object):
def removeZeroSumSublists(self, head):
"""
:param head: ListNode
:return: ListNode
"""
dummy = ListNode(-1) #用一个假头结点dummy返回结果,
dummy.next = head #防止头节点head被删除无法返回
pre_sum = 0 #记录前缀和
record = {0: dummy} # 用dict来存出现过的每个前缀和
# bug代码record = {0,dummy} 这里需要对record初始化{:}
while head:
pre_sum += head.val # bug代码 马虎没有写'+'
if pre_sum in record:
record[pre_sum].next = head.next #寻找是否有重复的元素
else: #类似于leetcode1:twoSum
record[pre_sum] = head
head = head.next
return dummy.next #用dummy来返回结果
"""
Wrong answer
Input
[1,3,2,-3,-2,5,5,-5,1]
Output
[1,5,5,-5,1]
Expected
[1,5,1]
"""
leeetcode1171 Remove Zero Sum Consecutive Nodes from Linked List的更多相关文章
- 【leetcode】1171. Remove Zero Sum Consecutive Nodes from Linked List
题目如下: Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum ...
- 【LeetCode】1171. Remove Zero Sum Consecutive Nodes from Linked List 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 preSum + 字典 日期 题目地址:https:/ ...
- [LintCode] Swap Two Nodes in Linked List 交换链表中的两个结点
Given a linked list and two values v1 and v2. Swap the two nodes in the linked list with values v1 a ...
- Swap Two Nodes in Linked List
Given a linked list and two values v1 and v2. Swap the two nodes in the linked list with values v1 a ...
- LintCode "Swap Two Nodes in Linked List"
Nothing special. Just take care of corner cases. class Solution { public: /** * @param head a ListNo ...
- [LeetCode] 83. Remove Duplicates from Sorted List_Easy tag: Linked List
Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...
- 算法与数据结构基础 - 链表(Linked List)
链表基础 链表(Linked List)相比数组(Array),物理存储上非连续.不支持O(1)时间按索引存取:但链表也有其优点,灵活的内存管理.允许在链表任意位置上插入和删除节点.单向链表结构一般如 ...
- [LeetCode] 82. Remove Duplicates from Sorted List II_Medium tag: Linked List
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinctnumbe ...
- [LeetCode] Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...
随机推荐
- 二十 Struts2的标签库,数据回显(基于值栈)
通用标签库 判断标签:<s:if>.<s:elseif>.<s:else> 循环标签:<s:iterator> 其他常用标签: <s:proper ...
- R语言 一个向量的值分派给另一个向量
group = sample(seq(1,10),size = 20,replace = T) #这20个组分别属于1,...,10 v = rnorm(length(unique(group)),0 ...
- [ 剑指Offer ] Week2 学习笔记
打印从1到最大的n位数 题解:由于未知n的大小,需要考虑大数问题.在这样的情况下,逐位地将字符串转换为数字输出,不会有溢出的可能.使用全排列的方式列出所有数字,省去了需要考虑进位的可能. 初始化数组, ...
- 「CSP-S模拟赛」2019第四场
「CSP-S模拟赛」2019第四场 T1 「JOI 2014 Final」JOI 徽章 题目 考场思考(正解) T2 「JOI 2015 Final」分蛋糕 2 题目 考场思考(正解) T3 「CQO ...
- jqGrid 复选框实现单选
参考:https://blog.csdn.net/java0311/article/details/45575517
- linux动态库(.so)和静态库(.a)的区别
静态库在程序编译时会被连接到目标代码中,程序运行时将不再需要该静态库.编译之后程序文件大,但加载快,隔离性也好. 动态库在程序编译时并不会被连接到目标代码中,而是在程序运行时才被载入,因此在程序运行时 ...
- 新闻网大数据实时分析可视化系统项目——3、Hadoop2.X分布式集群部署
(一)hadoop2.x版本下载及安装 Hadoop 版本选择目前主要基于三个厂商(国外)如下所示: 1.基于Apache厂商的最原始的hadoop版本, 所有发行版均基于这个版本进行改进. 2.基于 ...
- linux 串口通信 编程详解
计算机串口的引脚说明 序号 信号名称 符号 流向 功能 3 发送数据 TXD DTE→DCE DTE发送串行数据 2 接收数据 RXD DTE←DCE DTE 接收串行数据 7 请求发送 RTS DT ...
- java关于hasNext()
编写一段程序实现如果输入的一组数中含非整数数字,输出数字相加的和以及"attention"字符,如果全部是数字便输出数字的和. 程序1: package mian; import ...
- Day2-F-A Knight's Journey POJ-2488
Background The knight is getting bored of seeing the same black and white squares again and again an ...