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, node_2, node_3, ... etc. Each node may have a next larger value: for node_i, next_larger(node_i) is the node_j.val such that j > i, node_j.val > node_i.val, and j is the smallest possible choice. If such a j does not exist, the next larger value is 0. Return an array of integers answer, where answer[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] """
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution1(object):
def nextLargerNodes(self, head):
if not head.next:
return head if head.val != 0 else None #判断头节点是为否为空
nums = []
p = head
while(p): #将链表转为数组
nums.append(p.val)
p = p.next
stack = [] #创建一个栈
res = [0] * len(nums) #保存结果的数组
#bug 0 没有加[0]
for i, n in enumerate(nums): #
while stack and nums[stack[-1]] < n: #!!!单调递减的栈,栈中存的是索引
res[stack.pop()] = n
stack.append(i) #将索引压栈
return res
"""
runtime error
单调递减(增)栈,是一个非常普遍的解法
传送门https://blog.csdn.net/qq_17550379/article/details/86519771
""" """
我们也可以不将链表中的元素存放到一个list里面,
而是直接去处理链表,不过对于链表我们无法快速索引具体位置的值,
所以我们可以在stack中记录(index, val)数据对。
"""
class Solution2(object):
def nextLargerNodes(self, head):
res, stack = list(), list()
while head:
while stack and stack[-1][1] < head.val:
res[stack.pop()[0]] = head.val
stack.append([len(res), head.val])
res.append(0)
head = head.next
return res
leetcode1019 Next Greater Node In Linked List的更多相关文章
- LeetCode 1019. Next Greater Node In Linked List (链表中的下一个更大节点)
题目标签:Linked List, Stack 题目给了我们一个 Linked List,让我们找出对于每一个数字,它的下一个更大的数字. 首先把 Linked List 里的数字 存入 ArrayL ...
- [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, ...
- 【LeetCode】1019. Next Greater Node In Linked List 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减栈 日期 题目地址:https://leetc ...
- 【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: n ...
- Leetcode 1019. Next Greater Node In Linked List
单调栈的应用. class Solution: def nextLargerNodes(self, head: ListNode) -> List[int]: stack = [] ret = ...
- Lintcode225-Find Node in Linked List-Naive
225. Find Node in Linked List Find a node with given value in a linked list. Return null if not exis ...
- remove Nth Node from linked list从链表中删除倒数第n个元素
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- 算法与数据结构基础 - 链表(Linked List)
链表基础 链表(Linked List)相比数组(Array),物理存储上非连续.不支持O(1)时间按索引存取:但链表也有其优点,灵活的内存管理.允许在链表任意位置上插入和删除节点.单向链表结构一般如 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- Java基础知识笔记第一章:入门
java的地位: java具有面向对象,与平台无关,安全,稳定和多线程等优良特性,是目前软件设计中优秀的编程语言. java的特点: 1.简单 2.面向对象 3.平台无关 jre(java runti ...
- python 基础之字符串方法
字符串 print('chenxi'*8) 测试 D:\python\python.exe D:/untitled/dir/for.py chenxichenxichenxichenxichenxic ...
- web-pc项目中index页面分析
先上HTML代码: <%@ page language="java" contentType="text/html; charset=UTF-8" pag ...
- python--一起来盖个时间戳!!
1.datetime import datetime print(datetime.datetime.now()) 2.time import time otherStyleTime = time.s ...
- Hibernate(九)--N+1问题
1.在利用Hibernate操作数据库的时候,如果在实体类上设置了表的双向关联.这可能会出现Hibernate N+1的问题. 1.1.一对多: 在一方,查找得到了 n 个对象,那么又需要将 n 个对 ...
- 「POI2011」Meteors
「POI2011」Meteors 传送门 整体二分,树状数组实现区间修改单点查询,然后注意修改是在环上的. 参考代码: #include <cstdio> #include <vec ...
- Ubuntu 解决TXT文本乱码问题
只要依次在终端输入这两行指令即可: gsettings set org.gnome.gedit.preferences.encodings auto-detected "['GB18030' ...
- Python连载60-Tkinter布局、按钮以及属性详解
一.Tkinter 1.组件的大致使用步骤 (1)创建总面板 (2)创建面板上的各种组件: i.指定组件的父组件,即依附关系:ii.利用相应的属性对组件进行设置:iii.给组件安排布局. (3)同步 ...
- docker原理学习-环境搭建
1. mac下用VMware虚拟机安装ubunt16.04 2. ubuntu安装并启动ssh服务 3. 用mac终端ssh到虚拟机中 ssh didiyu@ip 输入登陆密码
- linux磁盘扩容常见问题
1.对于云主机可以对硬盘进行在线扩容,如果不方便重启服务器,可以键入以下命令系统能够马上识别新增空间: echo '1' > /sys/class/scsi_disk/0\:0\:0\:0/de ...