leetcode1019
class Solution(object):
def nextLargerNodes(self, head: ListNode) -> 'List[int]':
n = 0
temp = head
while temp != None:
n += 1
temp = temp.next
R = [0] * n
Stack = list()
index = 0
while head != None:
#print(head.val)
cur = head.val
if len(Stack) == 0:
Stack.append((index,cur))
else:
for i in range(len(Stack)-1,-1,-1):
peeknode = Stack[-1]
peekindex = peeknode[0]
peekval = peeknode[1]
if peekval < cur:
R[peekindex] = cur
Stack.remove((peekindex,peekval))
else:
break
Stack.append((index,cur))
head = head.next
index += 1 return R
使用一个栈结构用来记录数据,遍历整个链表,用当前节点的值“依次”与栈内各数字比较。
如果当前值>栈顶值,则栈顶元素进行“标记”,并出栈。一直到栈为空或者当前值<栈内值时,将当前值,入栈。
最后留在栈中的,都“标记”为0。这一步在初始化时就可以完成,因此最后就不用再处理了。
leetcode1019的更多相关文章
- [Swift]LeetCode1019. 链表中的下一个更大节点 | Next Greater Node In Linked List
We are given a linked list with head as the first node. Let's number the nodes in the list: node_1, ...
- leetcode1019 Next Greater Node In Linked List
""" We are given a linked list with head as the first node. Let's number the nodes in ...
随机推荐
- Hadoop概念学习系列之Hadoop集群动态增加新节点或删除已有某节点及复制策略导向 (四十三)
不多说,直接上干货! hadoop-2.6.0动态添加新节点 https://blog.csdn.net/baidu_25820069/article/details/52225216 Hadoop集 ...
- sudo 命令报错的解决方法
尝试着用终端打开Mac的安全权限(sudo spctl --master-disable),却显示以下提示,望高手解答. sudo: /etc/sudoers is world writablesud ...
- 【ZZ】详解哈希表的查找
详解哈希表的查找 https://mp.weixin.qq.com/s/j2j9gS62L-mmOH4p89OTKQ 详解哈希表的查找 2018-03-01 算法与数据结构 来自:静默虚空 http: ...
- checked和stop()的讲解
input:cheacked (属性选择器): checked 选中复选框 $("p").stop(ture); 代码的翻译:(参数)布尔值 p身上所有的动画都停止了 加不加tr ...
- windows server 2008 R2 安装
微软服务器操作系统大致有: server 2000(简称2K),还有server 2003(2K3),server 2008(2K8),server 2000和2003是基于NT内核的,而2008是基 ...
- JDK、JRE与JVM的关系
- idea关闭标签快捷键修改----兼 常用实用快捷键
还有一个快捷键: 自动补全返回值 : ctrl + alt + v alt + enter: 自动添加未定义的方法 idea 原本的关闭快捷键是: ctrl + F4,但是不好用,谁的手指伸这么长 修 ...
- dubbo协议下的单一长连接与多线程并发如何协同工作
上班的路上突然就冒出了这么个问题:既然在dubbo中描述消费者和提供者之间采用的是单一长连接,那么如果消费者端是高并发多线程模型的web应用,单一长连接如何解决多线程并发请求问题呢? 其实如果不太了解 ...
- Service Worker初体验
http://web.jobbole.com/84792/ http://imweb.io/topic/56592b8a823633e31839fc01
- golang "text/template" 模板语法简明教程
转自:https://www.cnblogs.com/Pynix/p/4154630.html [模板标签] 模板标签用"{{"和"}}"括起来 [注释] ...