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 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- VM安装linux操作系统详细教程
1.首先我们新建一个虚拟机,先不安装操作系统,稍后再对其安装Linux系统. 新建虚拟机步骤如下: 打开VMware软件,菜单栏点击“文件(F)”–>选择“新建虚拟机(N)”,如下图1,(或者直 ...
- 吴裕雄--天生自然PythonDjangoWeb企业开发:需求
开发或者做一个项目,是要有一个需求过来的,而不是无缘无故的,启动一个项目,或者推动整个项目进行下一步迭代.这个需求可能是根据用户反馈增加的,可能是老板提出来的,也有可能是产品经理提出来的,但是无论是什 ...
- shell脚本中执行shell脚本
1.a.sh #!/bin/sh name="hello" ./b.sh $name 2.b.sh(这里把b.sh与a.sh放在同一目录下,便于演示) #!/bin/sh ech ...
- 含有namespace的类型如何访问
下图中包含的String类型,如果想要在别的文件中去访问的话: 1)需要include"ApiClient.hpp" 2)需要使用oatpp::web::client::ApiCl ...
- IP show
1. 查看本机公网IP 1.1 curl ifconfig.me 1.2 ipinfo.io 1.3 test-ipv6.com 1.4 more 2. 查看本机IP,host 2.1 hostnam ...
- Mysql ,用户管理命令
添加用户.删除用户与授权以下对数据库的操作完全可以利用管理软件完成,比如在Navicat上进行操作,对数据库进行用户和权限管理. 1.创建用户:以root用户登录到数据库进行用户创建 命令: CREA ...
- python基础day05
上节内容变量if else注释 # ''' msg ''' 3个引号 打印多行 ', "" 双单引号的意义是一样的 缩进 本节内容pycharm使用 集成开发环境(IDE,Inte ...
- 获取Webshell方法总结
一.CMS获取Webshell方法 搜索CMS网站程序名称 eg:phpcms拿webshell.wordpress后台拿webshell 二.非CMS获取Webshell方法 2.1数据库备份获取W ...
- Ubuntu系统为MySQL开启远程连接
第一步:确保 Ubuntu 系统已经安装上了MySQL数据库.登陆数据库 ,运行如下命令: mysql -u 用户名 -p 然后输入密码,此时登录成功 第二步:创建用户用来远程连接,运行如下命令: G ...
- Eclipse传递main函数参数
在项目上右击 Run As->Run Configurations...->Arguments->在Program arguments:的文本框中输入你要传入的参数,若有几个参数则在 ...