zoukankan      html  css  js  c++  java
  • leetcode496——Next Greater Element I (JAVA)

    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:

      1. All elements in nums1 and nums2 are unique.
      2. The length of both nums1 and nums2 would not exceed 1000.

    我用两种方法解决这个问题。转载注明出处:http://www.cnblogs.com/wdfwolf3/,谢谢。

    1.时间复杂度O(m*n),8ms。思路很简单,对于每个子集合元素去遍历父集合,查找大于它的第一个元素。

    public int[] nextGreaterElement(int[] findNums, int[] nums) {
            //ans数组存放结果
            int[] ans = new int[findNums.length];
            //便利数组findNums中的元素
            for (int i = 0; i < findNums.length; i++) {
                int start = 0;
                //start索引从头开始遍历nums查找当前元素在nums中的位置
                while (findNums[i] != nums[start]){
                    start++;
                }
                //从start位置开始向后查找第一个比当前元素大的值,并赋值到ans数组中
                for (; start < nums.length; start++) {
                    if(nums[start] > findNums[i]){
                        ans[i] = nums[start];
                        break;
                    }
                }
                //如果start到达nums末尾,说明没有找到大于当前元素的值,赋值-1到ans数组中
                if(start == nums.length){
                    ans[i] = -1;
                }
            }
            return ans;
        }

    2.时间复杂度O(m*n),空间复杂度O(n),11ms。利用一个栈来查找父集合中每个元素的Next Greater Element,找到了就存放到HashMap中,最后遍历子集合,如果HashMap中没有说明不存在,赋值-1。

    public int[] nextGreaterElement(int[] findNums, int[] nums) {
            //辅助栈,存放待查找结果的元素,查找到的立即出栈
            Stack<Integer> stack = new Stack<>();
            //key存放元素,value存放找到的第一个大于它的值
            Map<Integer, Integer> map = new HashMap<>();
            //当栈顶元素大于当前元素时,入栈;当栈顶元素小于当前元素时,说明栈顶元素找到了第一个大于的值,出栈,然后继续出栈直到栈顶元素大于当前元素,将当前元素入栈。
            for (int i = 0; i < nums.length; i++) {
                while(!stack.isEmpty() && stack.peek() < nums[i]){
                    map.put(stack.pop(), nums[i]);
                }
                stack.push(nums[i]);
            }
            //ans数组存放结果
            int[] ans = new int[findNums.length];
         //遍历findNums,在map中查找结果,不存在说明没有大于它的第一个元素,赋值为-1
    for (int i = 0; i < findNums.length; i++) { ans[i] = map.getOrDefault(findNums[i], -1); } return ans; }
  • 相关阅读:
    Spring boot核心注解
    Spring-boot配置文件
    JAVA操作Excel
    操作系统之基础
    Batch Normalization
    解决ios微信页面回退不刷新的问题
    textarea高度自适应,随着内容增加高度增加
    旋转卡 可以用做登录注册
    一个页面tab标签切换,都有scroll事件的解决办法
    input type="radio" 赋值问题
  • 原文地址:https://www.cnblogs.com/wdfwolf3/p/6835566.html
Copyright © 2011-2022 走看看