496 Next Greater Element I 下一个更大元素 I
给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集。找到 nums1 中每个元素在 nums2 中的下一个比其大的值。
nums1 中数字 x 的下一个更大元素是指 x 在 nums2 中对应位置的右边的第一个比 x 大的元素。如果不存在,对应位置输出-1。
示例 1:
输入: nums1 = [4,1,2], nums2 = [1,3,4,2].
输出: [-1,3,-1]
解释:
对于num1中的数字4,你无法在第二个数组中找到下一个更大的数字,因此输出 -1。
对于num1中的数字1,第二个数组中数字1右边的下一个较大数字是 3。
对于num1中的数字2,第二个数组中没有下一个更大的数字,因此输出 -1。
示例 2:
输入: nums1 = [2,4], nums2 = [1,2,3,4].
输出: [3,-1]
解释:
对于num1中的数字2,第二个数组中的下一个较大数字是3。
对于num1中的数字4,第二个数组中没有下一个更大的数字,因此输出 -1。
注意:
1.nums1和nums2中所有元素是唯一的。
2.nums1和nums2 的数组大小都不超过1000。
详见:https://leetcode.com/problems/next-greater-element-i/description/
C++:
方法一:
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums)
{
vector<int> res(findNums.size());
for (int i = 0; i < findNums.size(); ++i)
{
int j = 0, k = 0;
for (; j < nums.size(); ++j)
{
if (nums[j] == findNums[i])
{
break;
}
}
for (k = j + 1; k < nums.size(); ++k)
{
if (nums[k] > nums[j])
{
res[i] = nums[k];
break;
}
}
if (k == nums.size())
{
res[i] = -1;
}
}
return res;
}
};
方法二:
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums)
{
vector<int> res(findNums.size());
unordered_map<int, int> m;
for (int i = 0; i < nums.size(); ++i)
{
m[nums[i]] = i;
}
for (int i = 0; i < findNums.size(); ++i)
{
res[i] = -1;
int start = m[findNums[i]];
for (int j = start + 1; j < nums.size(); ++j)
{
if (nums[j] > findNums[i])
{
res[i] = nums[j];
break;
}
}
}
return res;
}
};
参考:http://www.cnblogs.com/grandyang/p/6399855.html
496 Next Greater Element I 下一个更大元素 I的更多相关文章
- 503 Next Greater Element II 下一个更大元素 II
给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 x 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它 ...
- [leetcode]496. Next Greater Element I下一个较大元素
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...
- [LeetCode] 496. Next Greater Element I 下一个较大的元素 I
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...
- Leetcode496.Next Greater Element I下一个更大的元素1
给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的下一个比其大的值. nums1 中数字 x 的下一个更 ...
- 556. Next Greater Element III下一个更大的数字
[抄题]: Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exac ...
- [LeetCode] 503. Next Greater Element II 下一个较大的元素 II
Given a circular array (the next element of the last element is the first element of the array), pri ...
- [LeetCode] 556. Next Greater Element III 下一个较大的元素 III
Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...
- LeetCode 496. 下一个更大元素 I(Next Greater Element I) 35
496. 下一个更大元素 I 496. Next Greater Element I 题目描述 给定两个没有重复元素的数组 nums1 和 nums2,其中 nums1 是 nums2 的子集.找到 ...
- LeetCode 556. 下一个更大元素 III(Next Greater Element III)
556. 下一个更大元素 III 556. Next Greater Element III 题目描述 给定一个 32 位正整数 n,你需要找到最小的 32 位整数,其与 n 中存在的位数完全相同,并 ...
随机推荐
- [攻防实战]CTF大赛准备(手动注入sql)
一.IIS write漏洞利用 先用工具扫描,再上传小马,使用菜刀连接即可. 思考点: 如何获知是一台IIS站点? 本例中上传的一句话木马是什么意思? <%eval request(" ...
- poj 2228 Naptime(DP的后效性处理)
\(Naptime\) \(solution:\) 这道题不做多讲,它和很多区间DP的套路一致,但是这一道题它不允许断环成链,会超时.但是我们发现如果这只奶牛跨夜休息那么它在不跨夜的二十四个小时里一定 ...
- 分段控制器--UISegmentedControl 基本用法
http://blog.csdn.net/heng615975867/article/details/43527295 http://blog.csdn.net/gf771115/article/de ...
- SPOJ:Divisors of factorial (hard) (唯一分解&分块优化)
Factorial numbers are getting big very soon, you'll have to compute the number of divisors of such h ...
- [Java] public, private, protected
同包不同类的成员 不同包中子类的成员 不同包非子类的成员 public √ √ √ protected √ √ × 默认 √ × × private × × ×
- 为什么python2.7中用Process创建子进程的语句之前必须加#if
from multiprocessing import Process import os def run(name): print 'The child process '%s' (pid %d) ...
- curl测试dns解析时间及tcp连接时间
1.用Linux下的curl命令测量网络请求(分号是分隔符,可以是其他符号): curl -o /dev/null -s -w %{time_connect}:%{time_starttransfer ...
- hibernate的基础学习--一对多关联
基本的用户和部门类,只有uuid和名称,没有其余字段. 配置文件 部门: <?xml version="1.0" encoding="utf-8" ?&g ...
- git提交历史不显示自己账户的问题
其实这个问题本身也不大好描述.还是上图好了 问题就如同途中倒数第二次提交.明明就是我提交的,却不显示我的账号.个人页面也不反映这次提交. 后来发现问题所在,要想正确显示,必须要把对这个项目的邮箱设 ...
- 洛谷 - P2578 - 九数码游戏 - bfs
https://www.luogu.org/problemnew/show/P2578 一个挺搞的东西,用康托展开做记忆化搜索可以少一个log的查询. #include <bits/stdc++ ...