美文网首页
LeetCode关于Next Greater Element的问

LeetCode关于Next Greater Element的问

作者: 专职跑龙套 | 来源:发表于2018-04-02 17:36 被阅读37次

    关于我的 Leetcode 题目解答,代码前往 Github:https://github.com/chenxiangcyr/leetcode-answers


    LeetCode题目:496. Next Greater Element I
    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.

    The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number.

    Example 1:
    Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
    Output: [-1,3,-1]
    Explanation:
    For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
    For number 1 in the first array, the next greater number for it in the second array is 3.
    For number 2 in the first array, there is no next greater number for it in the second array, so output -1.

    Example 2:
    Input: nums1 = [2,4], nums2 = [1,2,3,4].
    Output: [3,-1]
    Explanation:
    For number 2 in the first array, the next greater number for it in the second array is 3.
    For number 4 in the first array, there is no next greater number for it in the second array, so output -1.

    Note:

    • All elements in nums1 and nums2 are unique.
    • The length of both nums1 and nums2 would not exceed 1000.
    class Solution {
        public int[] nextGreaterElement(int[] nums1, int[] nums2) {
            
            /* Time Complexity O(m * n)
            int[] result = new int[nums1.length];
            Arrays.fill(result, -1);
            
            for(int i = 0; i < nums1.length; i++) {
                boolean foundEqual = false;
                
                for(int j = 0; j < nums2.length; j++) {
                    
                    if(nums1[i] == nums2[j]) {
                        foundEqual = true;
                    }
                    
                    if(nums1[i] < nums2[j] && foundEqual == true) {
                        result[i] = nums2[j];
                        break;
                    }
                }
            }
            
            return result;
            */
            
            /*
            Time Complexity O(m + n)
            */
            
            Stack<Integer> stack = new Stack<>();
            
            // key: 数字,value:next Greater Element
            Map<Integer, Integer> map = new HashMap<>();
            
            for(int num : nums2) {
                while(!stack.isEmpty() && num > stack.peek()) {
                    int t = stack.pop();
                    map.put(t, num);
                }
                
                stack.push(num);
            }
            
            while(!stack.isEmpty()) {
                int t = stack.pop();
                map.put(t, -1);
            }
            
            int[] result = new int[nums1.length];
            for(int i = 0; i < nums1.length; i++) {
                result[i] = map.get(nums1[i]);
            }
            
            return result;
        }
    }
    

    LeetCode题目:503. Next Greater Element II
    Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order next in the array, which means you could search circularly to find its next greater number. If it doesn't exist, output -1 for this number.

    Example 1:
    Input: [1,2,1]
    Output: [2,-1,2]
    Explanation: The first 1's next greater number is 2;
    The number 2 can't find next greater number;
    The second 1's next greater number needs to search circularly, which is also 2.

    Note: The length of given array won't exceed 10000.

    class Solution {
        public int[] nextGreaterElements(int[] nums) {
            if(nums == null || nums.length == 0) return nums;
            
            // 存储数字idx
            Stack<Integer> stack = new Stack<>();
            
            // key: 数字idx,value:next Greater Element
            Map<Integer, Integer> map = new HashMap<>();
            
            for(int i = 0; i < 2 * nums.length; i++) {
                while(!stack.isEmpty() && nums[i % nums.length] > nums[stack.peek()]) {
                    int t = stack.pop();
                    map.put(t, nums[i % nums.length]);
                }
                
                if (i < nums.length) stack.push(i);
            }
            
            while(!stack.isEmpty()) {
                int t = stack.pop();
                map.put(t, -1);
            }
            
            int[] result = new int[nums.length];
            for(int i = 0; i < nums.length; i++) {
                result[i] = map.get(i);
            }
            
            return result;
        }
    }
    

    LeetCode题目:556. Next Greater Element III
    Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly the same digits existing in the integer n and is greater in value than n. If no such positive 32-bit integer exists, you need to return -1.

    Example 1:
    Input: 12
    Output: 21

    Example 2:
    Input: 21
    Output: -1

    思路参见 https://leetcode.com/problems/next-greater-element-iii/discuss/101824/Simple-Java-solution-(4ms)-with-explanation.

    class Solution {
        public int nextGreaterElement(int n) {
            char[] number = (n + "").toCharArray();
            
            /*
            从右边开始,找到第一个比右边元素小的
            例如:534976,找到了数字4对应的位置2
            */
            int i = number.length - 1;
            while(i > 0 && number[i-1] >= number[i]) {
                i--;
            }
    
            // 表明所有数字已经是升序排列了,得不到结果
            if (i == 0) {
                return -1;
            }
            
            i--; // 4的位置
            
            // 找到4的右边比4大的所有元素中,最小的一个,此时找到了6
            int x = number[i];
            int smallest = i + 1;
            for (int j = i + 1; j < number.length; j++) {
                if (number[j] > x && number[j] <= number[smallest]) {
                    smallest = j;
                }
            }
            
            // 交换 4 和 6,得到536974
            char temp = number[i];
            number[i] = number[smallest];
            number[smallest] = temp;
            
            // 将6的后半部分974重新排序,得到 536479
            Arrays.sort(number, i + 1, number.length);
            
            long val = Long.parseLong(new String(number));
            return (val <= Integer.MAX_VALUE) ? (int) val : -1;
        }
    }
    

    相关文章

      网友评论

          本文标题:LeetCode关于Next Greater Element的问

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