跳到主要内容
极客日志极客日志面向AI+效率的开发者社区
首页博客GitHub 精选镜像工具UI配色美学隐私政策关于联系
搜索内容 / 工具 / 仓库 / 镜像...⌘K搜索
注册
博客列表
C++算法

Next Greater Element I 问题解析与实现

Next Greater Element I 题目要求在 nums2 中找到 nums1 每个元素右侧第一个更大的数,不存在则返回 -1。nums1 是 nums2 的子集且无重复。解题逻辑为遍历 nums1 元素,在 nums2 中定位后向后搜索。C++ 实现利用迭代器查找并处理边界情况。

GRACE Grace发布于 2025/1/14更新于 2026/6/1220 浏览
Next Greater Element I 问题解析与实现

You are given two arrays (without duplicates) nums1 and nums2 where nums1's elements are a 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.

思路

num1 是 num2 的子集,从 num2 中找到 num1 中指定元素的下一个较大的值并返回,若没有,则返回 -1。循环获取 nums1 中的每个元素,并在 nums2 中查找返回小标迭代器,然后记录下位置按规则向后遍历寻找。

class Solution {
public:
    vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
        vector<int> res;
        for(auto &elem : findNums) {
            auto it=find(nums.begin(),nums.end(),elem);
            if(++it==nums.()) {
                res.();
            }  {
                (;it!=nums.();++it) {
                    (*it>elem) {
                        res.(*it);
                        ;
                    }
                }
                (it==nums.()) res.();
            }
        }
         res;
    }
};
end
push_back
-1
else
for
end
if
push_back
break
if
end
push_back
-1
return

目录

  1. 思路
  • 💰 8折买阿里云服务器限时8折了解详情
  • Magick API 一键接入全球大模型注册送1000万token查看
  • 🤖 一键搭建Deepseek满血版了解详情
  • 一键打造专属AI 智能体了解详情
极客日志微信公众号二维码

微信扫一扫,关注极客日志

微信公众号「极客日志V2」,在微信中扫描左侧二维码关注。展示文案:极客日志V2 zeeklog

更多推荐文章

查看全部
  • 华为机试经典算法题整理与实战解析
  • PCL 编译报错 index_t 未定义及官方 Demo 版本适配指南
  • Rock 5B+ 部署 MAVSDK 控制无人机及 WSL 仿真环境搭建
  • Flowise 结合 Web Scraping 的数据采集流程
  • Linux 网络编程实战:基于 C++ 实现 JSON 与 HTTP Web 服务器
  • Java 后端工程师转行 AI 大模型岗位指南:优势与学习路线
  • Python 语言概述:核心特性、应用场景与学习价值
  • Actix Web框架全面指南:Rust Web开发实战
  • Vue Element UI 日历组件实现日程安排与区间查询
  • 如何在 Ubuntu 上配置 OpenClaw 实现文件创建功能
  • Cursor + Figma MCP:AI 自动化还原设计稿实战
  • 基于 Multi-Agent、Skills 与 Spring AI 构建自主决策智能体
  • 算法进阶:前缀和的应用场景与细节
  • 老款 Intel Mac 部署 OpenClaw 极简指南
  • 工业视觉缺陷检测算法总结:从传统到深度学习,5 类核心算法
  • llama.cpp 技术指南:从底层原理到实战部署
  • Android 热修复技术原理与主流方案对比
  • 华为盘古大模型 3.0 发布:架构解析与行业应用分析
  • Qt Creator 配置 GitHub Copilot AI 编程插件
  • Linux 环境下 C/C++ 编译流程详解

相关免费在线工具

  • 加密/解密文本

    使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online

  • Gemini 图片去水印

    基于开源反向 Alpha 混合算法去除 Gemini/Nano Banana 图片水印,支持批量处理与下载。 在线工具,Gemini 图片去水印在线工具,online

  • Base64 字符串编码/解码

    将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online

  • Base64 文件转换器

    将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online

  • Markdown转HTML

    将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML转Markdown 互为补充。 在线工具,Markdown转HTML在线工具,online

  • HTML转Markdown

    将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML转Markdown在线工具,online