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 ...
随机推荐
- STL基础--算法(不修改数据的算法)
不修改数据的算法 count, min and max, compare, linear search, attribute // 算法中Lambda函数很常用: num = count_if(vec ...
- Python利用Plotly实现对MySQL中的数据可视化
Mysql表数据: demo.sql内容 create table demo( id int ,product varchar(50) ,price decimal(18,2) ,quantity i ...
- 记使用vue-awesome-swiper遇到的一些问题
一.vue-awesome-swiper的使用 1.在项目中全局引用 import VueAwesomeSwiper from 'vue-awesome-swiper' // require s ...
- MIME Types - Complete List
Suffixes applicable Media type and subtype(s) .3dm x-world/x-3dmf .3dmf x-world/x-3dmf .a applicatio ...
- 服务容错保护断路器Hystrix之五:配置
接着<服务容错保护断路器Hystrix之二:Hystrix工作流程解析>中的<2.8.关于配置>再列举重要的配置如下 一.hystrix在生产中的建议 1.保持timeout的 ...
- 服务容错保护断路器Hystrix之一:入门示例介绍(springcloud引入Hystrix的两种方式)
限流知识<高可用服务设计之二:Rate limiting 限流与降级> 在微服务架构中,我们将系统拆分成了一个个的服务单元,各单元间通过服务注册与订阅的方式互相依赖.由于每个单元都在不同的 ...
- C语言强化——数组
打印九九乘法表 #include<stdio.h> int main() { int num = 1; for (int i = 1;i <= 9;++i) { for (int j ...
- https证书的验证过程与生成方法
1.简洁的解释: 1.服务器 用RSA生成公钥和私钥2.把公钥放在证书里发送给客户端,私钥自己保存3.客户端首先向一个权威的服务器检查证书的合法性,如果证书合法,客户端产生一段随机数,这个随机数就作为 ...
- URL传值乱码
JS端: &value=encodeURIComponent("value") C端: HttpUtility.UrlDecode(Request.Params[" ...
- spring 配置文件的头部 xmls
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w ...