美文网首页程序员代码面试
[LeetCode] Next Greater Node In

[LeetCode] Next Greater Node In

作者: 埋没随百草 | 来源:发表于2019-05-02 22:43 被阅读0次

    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]

    Note:

    1 <= node.val <= 10^9 for each node in the linked list.
    The given list has length in the range [0, 10000].

    解题思路

    • 思路1:对于每个节点,遍历其后的节点,找到第一个大于它的节点。
    • 思路2:先把链表转为数组,然后借助栈来实现。当栈为空或者当前数组元素不大于栈顶指针所指向的元素时,把当前元素的下表入栈,否则依次把栈顶指针出站,并把相应位置的值设为数组的当前元素。

    实现代码

    实现1:

    // Runtime: 1115 ms, faster than 5.12% of Java online submissions for Next Greater Node In Linked List.
    // Memory Usage: 42.8 MB, less than 100.00% of Java online submissions for Next Greater Node In Linked List.
    /**
     * 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> result = new ArrayList<>();
            while (head != null) {
                ListNode cur = head;
                while (head != null && head.val <= cur.val) {
                    head = head.next;
                }
    
                if (head != null) {
                    result.add(head.val);
                } else {
                    result.add(0);
                }
                head = cur.next;
            }
            
            int[] nums = new int[result.size()];
            for (int i = 0; i < result.size(); i++) {
                nums[i] = result.get(i);
            }
            return nums;
        }
    }
    

    实现2:

    // Runtime: 39 ms, faster than 64.33% of Java online submissions for Next Greater Node In Linked List.
    // Memory Usage: 42.6 MB, less than 100.00% of Java online submissions for Next Greater Node In Linked List.
    /**
     * 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> nums = new ArrayList<>();
            for (ListNode node = head; node != null; node = node.next) {
                nums.add(node.val);
            }
            
            int[] res = new int[nums.size()];
            Stack<Integer> stack = new Stack<>();
            for (int i = 0; i < nums.size(); i++) {
                while (!stack.empty() && nums.get(i) > nums.get(stack.peek())) {
                    res[stack.pop()] = nums.get(i);
                }
                stack.push(i);
            }
            return res;
        }
    }
    

    相关文章

      网友评论

        本文标题:[LeetCode] Next Greater Node In

        本文链接:https://www.haomeiwen.com/subject/fcvonqtx.html