IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> LeetCode-496. Next Greater Element I [C++][Java] -> 正文阅读

[数据结构与算法]LeetCode-496. Next Greater Element I [C++][Java]

LeetCode-496. Next Greater Element Iicon-default.png?t=M3K6https://leetcode.com/problems/next-greater-element-i/

The?next greater element?of some element?x?in an array is the?first greater?element that is?to the right?of?x?in the same array.

You are given two?distinct 0-indexed?integer arrays?nums1?and?nums2, where?nums1?is a subset of?nums2.

For each?0 <= i < nums1.length, find the index?j?such that?nums1[i] == nums2[j]?and determine the?next greater element?of?nums2[j]?in?nums2. If there is no next greater element, then the answer for this query is?-1.

Return?an array?ans?of length?nums1.length?such that?ans[i]?is the?next greater element?as described above.

Example 1:

Input: nums1 = [4,1,2], nums2 = [1,3,4,2]
Output: [-1,3,-1]
Explanation: The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [1,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1.

Example 2:

Input: nums1 = [2,4], nums2 = [1,2,3,4]
Output: [3,-1]
Explanation: The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,2,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,4]. There is no next greater element, so the answer is -1.

Constraints:

  • 1 <= nums1.length <= nums2.length <= 1000
  • 0 <= nums1[i], nums2[i] <= 10^4
  • All integers in?nums1?and?nums2?are?unique.
  • All the integers of?nums1?also appear in?nums2.

【C++】

1. vector

class Solution {
public:
    vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
        int m = nums1.size();
        int n = nums2.size();
        vector<int> res(m);
        for (int i = 0; i < m; ++i) {
            int j = 0;
            while (j < n && nums2[j] != nums1[i]) {++j;}
            int k = j + 1;
            while (k < n && nums2[k] < nums2[j]) {++k;}
            res[i] = k < n ? nums2[k] : -1;
        }
        return res;
    }
};

2. stack

class Solution {
public:
    vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
        stack<int> st;
        int n = nums2.size();
        map<int,int> v;
        for (int i = n-1; i>=0; i--) {
            if (st.empty()) {
                v[nums2[i]] = -1;
                st.push(nums2[i]);
            } else {
                while (!st.empty() && st.top() <= nums2[i]) {st.pop();}
                if (st.empty()) {v[nums2[i]] = -1;}
                else {v[nums2[i]] = st.top();}
                st.push(nums2[i]);
            }
        }
        n = nums1.size();
        vector<int> ans(n);
        for (int i=0;i<n;i++) {ans[i] = v[nums1[i]];}
        return ans;
    }
};

【Java】

class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        int n1 = nums1.length;
        int n2 = nums2.length;
        int[] ans = new int[n1];
        // To hold the numbers in the right which can be greater than current number
        Stack<Integer> stack = new Stack<>();
        // To store the mapping of number with it's greater number in right
        Map<Integer,Integer> greaterInRight = new HashMap<>();
        // Iterate from last number to the first number
        for(int i=n2-1; i>=0; i--) {
            // Remove numbers from top of the stack until a greater number is found
            while( !stack.isEmpty() && stack.peek() < nums2[i] ) {
                stack.pop();
            }
            /* If a stack is not empty, then the top of the stack contains the greater
            number than current number, else there is no number in right which is 
            greater than current number */
            greaterInRight.put(nums2[i], stack.isEmpty() ? -1 : stack.peek() );
            /* Push current number into stack, as this number can be greater number
            for the numbers in left */
            stack.push( nums2[i] );
        }
        // Use the map to get the number in right greater than each number in {nums1}
        for(int i=0; i<n1; i++) {
            ans[i] = greaterInRight.get( nums1[i] );
        }
        return ans;
    }
}

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2022-05-05 11:44:27  更:2022-05-05 11:48:27 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 5:39:17-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码