1019:链表中的下一个更大节点
This commit is contained in:
parent
8dfe5d64e9
commit
602b60c36d
@ -0,0 +1,95 @@
|
||||
//给出一个以头节点 head 作为第一个节点的链表。链表中的节点分别编号为:node_1, node_2, node_3, ... 。
|
||||
//
|
||||
// 每个节点都可能有下一个更大值(next larger value):对于 node_i,如果其 next_larger(node_i) 是 node_j.
|
||||
//val,那么就有 j > i 且 node_j.val > node_i.val,而 j 是可能的选项中最小的那个。如果不存在这样的 j,那么下一个更大值为 0
|
||||
// 。
|
||||
//
|
||||
// 返回整数答案数组 answer,其中 answer[i] = next_larger(node_{i+1}) 。
|
||||
//
|
||||
// 注意:在下面的示例中,诸如 [2,1,5] 这样的输入(不是输出)是链表的序列化表示,其头节点的值为 2,第二个节点值为 1,第三个节点值为 5 。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
// 输入:[2,1,5]
|
||||
//输出:[5,5,0]
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
// 输入:[2,7,4,3,5]
|
||||
//输出:[7,0,5,5,0]
|
||||
//
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
// 输入:[1,7,5,1,9,2,5,1]
|
||||
//输出:[7,9,9,9,0,5,0,0]
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 对于链表中的每个节点,1 <= node.val <= 10^9
|
||||
// 给定列表的长度在 [0, 10000] 范围内
|
||||
//
|
||||
// Related Topics 栈 链表
|
||||
// 👍 156 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import com.code.leet.entiy.ListNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
//1019:链表中的下一个更大节点
|
||||
public class NextGreaterNodeInLinkedList{
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new NextGreaterNodeInLinkedList().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode(int x) { val = x; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public int[] nextLargerNodes(ListNode head) {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
List<Integer> result = new ArrayList<>();
|
||||
List<Integer> indexs = new ArrayList<>();
|
||||
int max = 0;
|
||||
int index =0;
|
||||
while (head != null) {
|
||||
indexs.add(index);
|
||||
if (head.val > max) {
|
||||
int size = indexs.size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
if(head.val>result.get(indexs.get(i))){
|
||||
result.set(indexs.get(i),head.val);
|
||||
indexs.remove(indexs.get(i));
|
||||
}
|
||||
}
|
||||
max = head.val;
|
||||
}
|
||||
index++;
|
||||
result.add(0);
|
||||
list.add(head.val);
|
||||
head = head.next;
|
||||
}
|
||||
result.add(0);
|
||||
return result.stream().mapToInt(Integer::valueOf).toArray();
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
<p>给出一个以头节点 <code>head</code> 作为第一个节点的链表。链表中的节点分别编号为:<code>node_1, node_2, node_3, ...</code> 。</p>
|
||||
|
||||
<p>每个节点都可能有下一个更大值(<em>next larger</em> <strong>value</strong>):对于 <code>node_i</code>,如果其 <code>next_larger(node_i)</code> 是 <code>node_j.val</code>,那么就有 <code>j > i</code> 且 <code>node_j.val > node_i.val</code>,而 <code>j</code> 是可能的选项中最小的那个。如果不存在这样的 <code>j</code>,那么下一个更大值为 <code>0</code> 。</p>
|
||||
|
||||
<p>返回整数答案数组 <code>answer</code>,其中 <code>answer[i] = next_larger(node_{i+1})</code> 。</p>
|
||||
|
||||
<p><strong><em>注意:</em></strong>在下面的示例中,诸如 <code>[2,1,5]</code> 这样的<strong>输入</strong>(不是输出)是链表的序列化表示,其头节点的值为 2,第二个节点值为 1,第三个节点值为 5 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>[2,1,5]
|
||||
<strong>输出:</strong>[5,5,0]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>[2,7,4,3,5]
|
||||
<strong>输出:</strong>[7,0,5,5,0]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>[1,7,5,1,9,2,5,1]
|
||||
<strong>输出:</strong>[7,9,9,9,0,5,0,0]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ol>
|
||||
<li>对于链表中的每个节点,<code>1 <= node.val <= 10^9</code></li>
|
||||
<li>给定列表的长度在 <code>[0, 10000]</code> 范围内</li>
|
||||
</ol>
|
||||
<div><div>Related Topics</div><div><li>栈</li><li>链表</li></div></div>\n<div><li>👍 156</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user