当前位置:网站首页>力扣每日一题-第25天-496.下一个更大元素Ⅰ
力扣每日一题-第25天-496.下一个更大元素Ⅰ
2022-06-24 19:24:00 【重邮研究森】
2022.6.23今天你刷题了吗?
题目:
nums1 中数字 x 的 下一个更大元素 是指 x 在 nums2 中对应位置 右侧 的 第一个 比 x 大的元素。
给你两个 没有重复元素 的数组 nums1 和 nums2 ,下标从 0 开始计数,其中nums1 是 nums2 的子集。
对于每个 0 <= i < nums1.length ,找出满足 nums1[i] == nums2[j] 的下标 j ,并且在 nums2 确定 nums2[j] 的 下一个更大元素 。如果不存在下一个更大元素,那么本次查询的答案是 -1 。
返回一个长度为 nums1.length 的数组 ans 作为答案,满足 ans[i] 是如上所述的 下一个更大元素
分析:
给定两个数组,例如
num1【1,2,3】,num2【1,2,4,3】,则num1的【1】对应num2的【2】,num1的2对应num2的【4】,num1的【3】则无对应,然后返回【2,4,-1】
也就是说对于数组一中的元素,我们要在数组2中去找是否存在一个元素满足下面条件
1.在num2下该必须是元素右方存在第一个一个元素
2.且该元素大于数组一中这个素
思路就是暴力求解,遍历两个数组,在第一层循环中去找num1的值,看第二层循环是否存在一个满足该要求的元素。
解析:
1.暴力求解
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
vector<int>vec;
int flag = 0;
for (auto i = 0; i < nums1.size(); ++i)
{
for (auto j = 0; j < nums2.size(); ++j)
{
if (nums1[i] == nums2[j])
{
flag = 1;
}
if (flag == 1&&nums1[i]<nums2[j])
{
vec.push_back(nums2[j]);
flag = 0;
break;
}
if (flag == 1 && nums1[i] >= nums2[j]&&j==nums2.size()-1)
{
vec.push_back(-1);
flag = 0;
break;
}
}
}
return vec;
}
};2.单调栈
该类方法用于解决数组中下一个元素更大类型。
思想就是利用一个栈去维护一个从小到大排列的栈,然后把数据插入map中,再把map插入vector中(个人感觉还是很难)。其中如果在栈插入时遇到更大的值,就要进行出栈,这个方法还要多练!
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
unordered_map<int, int> hashmap;
stack<int> st;
for (int i = nums2.size() - 1; i >= 0; --i)
{
int num = nums2[i];
while (!st.empty() && num >= st.top())
{
st.pop();
}
hashmap[num] = st.empty() ? -1 : st.top();
st.push(num);
}
vector<int>res(nums1.size());
for (auto i = 0; i < nums1.size(); ++i)
{
res[i] = hashmap[nums1[i]];
}
return res;
}
};边栏推荐
- Analysis of tcpdump packet capturing kernel code
- DHCP operation
- Alibaba cloud lightweight servers open designated ports
- 装修首页自定义全屏视频播放效果gif动态图片制作视频教程播放代码操作设置全屏居中阿里巴巴国际站
- 使用 Go 编程语言 66 个陷阱:Golang 开发者的陷阱和常见错误指北
- Static routing job
- ping: www.baidu. Com: unknown name or service
- Web project deployment
- Adding subscribers to a list using mailchimp's API V3
- Pytest testing framework
猜你喜欢

Memcached comprehensive analysis – 3 Deletion mechanism and development direction of memcached

为什么生命科学企业都在陆续上云?

123. the best time to buy and sell shares III

Multi view function in blender

VirtualBox virtual machine installation win10 Enterprise Edition

Return of missing persons

一文理解OpenStack网络

CondaValueError: The target prefix is the base prefix. Aborting.

去掉录屏提醒(七牛云demo)

Simple analysis of WordPress architecture
随机推荐
The first day of handwritten RPC -- review of some basic knowledge
Apple mobile phone can see some fun ways to install IPA package
Memcached comprehensive analysis – 5 Memcached applications and compatible programs
Page replacement of virtual memory paging mechanism
Common self realization functions in C language development
Functional analysis of ebpf sockops
Pyaudio audio recording
Analysis of tcpdump packet capturing kernel code
Memcached full profiling – 1 Fundamentals of memcached
EditText controls the soft keyboard to search
架构实战营 第 6 期 毕业总结
TCP specifies the source port
Curl command
Pattern recognition - 9 Decision tree
基于C语言实现的足球信息查询系统 课程报告+项目源码+演示PPT+项目截图
Golang reflection operation collation
The virtual currency evaporated $2trillion in seven months, and the "musks" ended the dream of 150000 people becoming rich
VirtualBox虚拟机安装Win10企业版
Why are life science enterprises on the cloud in succession?
EditText 控制软键盘出现 搜索