【leetcode】1019. 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, node_2, node_3, ...
etc.Each node may have a next larger value: for
node_i
,next_larger(node_i)
is thenode_j.val
such thatj > i
,node_j.val > node_i.val
, andj
is the smallest possible choice. If such aj
does not exist, the next larger value is0
.Return an array of integers
answer
, whereanswer[i] = next_larger(node_{i+1})
.Note that in the example inputs (not outputs) below, arrays such as
[2,1,5]
represent the serialization of a linked list with a head node value of 2, second node value of 1, and third node value of 5.Example 1:
Input: [2,1,5]
Output: [5,5,0]Example 2:
Input: [2,7,4,3,5]
Output: [7,0,5,5,0]Example 3:
Input: [1,7,5,1,9,2,5,1]
Output: [7,9,9,9,0,5,0,0]Note:
1 <= node.val <= 10^9
for each node in the linked list.- The given list has length in the range
[0, 10000]
.
解题思路:本题是找出离自己最近的大于自己的数,和以前做过的 【leetcode】84. Largest Rectangle in Histogram 非常相似,区别在于【leetcode】84. Largest Rectangle in Histogram 是找出最近的比自己小的数,但是原理是一样的。我的解法就是把链表转成list,然后参照【leetcode】84. Largest Rectangle in Histogram的解法。
代码如下:
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def nextLargerNodes(self, head):
"""
:type head: ListNode
:rtype: List[int]
"""
val = []
while head != None:
val.append(head.val)
head = head.next
res = [0] * len(val)
for i in range(len(val)-2,-1,-1):
next = i+1
while res[next] != 0 and val[i] >= val[next] :
next = res[next]
if val[i] >= val[next]:
res[i] = 0
else:
res[i] = next
#print res
for i in range(len(res)):
if res[i] == 0:
continue
res[i] = val[res[i]]
return res
【leetcode】1019. Next Greater Node In Linked List的更多相关文章
- 【LeetCode】1019. Next Greater Node In Linked List 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减栈 日期 题目地址:https://leetc ...
- 【LeetCode】556. Next Greater Element III 解题报告(Python)
[LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- 【LeetCode】430. Flatten a Multilevel Doubly Linked List 解题报告(Python)
[LeetCode]430. Flatten a Multilevel Doubly Linked List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...
- LeetCode 1019. Next Greater Node In Linked List (链表中的下一个更大节点)
题目标签:Linked List, Stack 题目给了我们一个 Linked List,让我们找出对于每一个数字,它的下一个更大的数字. 首先把 Linked List 里的数字 存入 ArrayL ...
- 【LeetCode】496. Next Greater Element I 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接遍历查找 字典保存位置 日期 题目地址:http ...
- 【LeetCode】671. Second Minimum Node In a Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找出所有值再求次小值 遍历时求次小值 日期 题目地址 ...
- 【LeetCode】503. Next Greater Element II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解法 单调递减栈 日期 题目地址:https:/ ...
- 【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣 ...
- 【LeetCode】237 & 203 - Delete Node in a Linked List & Remove Linked List Elements
237 - Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly l ...
随机推荐
- pytest相关问题解析
1. 如果你想查询在你的环境下有哪些pytest的active plugin可以使用: py.test --traceconfig 会得到一个扩展的头文件名显示激活的插件和他们的名字.同时也会打印出当 ...
- STL容器内数据删除
STL中的容器按存储方式分为两类,一类是按以数组形式存储的容器(如:vector .deque):另一类是以不连续的节点形式存储的容器(如:list.set.map).在使用erase方法来删除元素时 ...
- (转)Window 中杀死指定端口 cmd 命令行 taskkill
Windows平台 两步方法 : 1 查询端口占用,2 强行杀死进程 netstat -aon|findstr "8080" taskkill /pid 4136-t -f ...
- Mybatis入门之MyBatis基础
一.MyBatis概述 1.ORM模型简介 ORM:对象关系映射(Object Relation Mapping) 1)传统JDBC程序的设计缺陷(实际项目不使用) a.大量配置信息硬编码 b.大量的 ...
- Vagrant 手册之 Provisioning - Shell 配置程序
原文地址 Provisioner 命令:"shell" 示例: node.vm.provision "shell" do |s| s.inline = < ...
- Grafana+Prometheus系统监控之Redis
REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统. Redis是一个开源的使用ANSI C语言编写.遵守B ...
- Recurrent Neural Network(2):BPTT and Long-term Dependencies
在RNN(1)中,我们将带有Reccurent Connection的node依照时间维度展开成了如下的形式: 在每个时刻t=0,1,2,3,...,神经网络的输出都会产生error:E0,E1,E2 ...
- Requests爬取网页的编码问题
Requests爬取网页的编码问题 import requests from requests import exceptions def getHtml(): try: r=requests.get ...
- spring的4种事务特性,4种隔离级别,7种传播行为
spring事务: 什么是事务: 事务逻辑上的一组操作,组成这组操作的各个逻辑单元,要么一起成功,要么一起失败. 事务特性(4种): 原子性 (atomicity):强调事务的不可分割. 一致性 (c ...
- ReplaceAll 特殊字符处理
用到Json与replaceAll Http拦截脚本中经常需要替换,replace虽然不需要处理特殊字符,但是不能匹配多个,ReplaceAll能够使用正则,不过需要处理的转移实在太多 比如,需要替换 ...