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 中存在的位数完全相同,并 ...
随机推荐
- Lily HBase Indexer同步HBase二级索引到Solr丢失数据的问题分析
一.问题描述二.分析步骤2.1 查看日志2.2 修改Solr的硬提交2.3 寻求StackOverFlow帮助2.4 修改了read-row="never"后,丢失部分字段2.5 ...
- ermissions on /usr/local/mongodb/conf/keyFile are too open
ermissions on /usr/local/mongodb/conf/keyFile are too open > rs.initiate(cfg); { "ok" : ...
- what's WSDL
WSDL (Web Services Description Language,Web服务描述语言) 它是一种XML Application,他将Web服务描述定义为一组服务访问点,客户端可以通过这些 ...
- 将异常(getStackTrace)转化成String
方法一: private static String getStackMsg(Exception e) { StringBuffer sb = new StringBuffer(); StackTra ...
- (linux)SD卡初始化-mmc_sd_init_card函数
为了学习SD/SDIO协议,看了一下linux中初始化SD卡的流程,结合代码更容易SD初始化是怎么做的. 下面图截自:"SD Specifications Part 1 Physical ...
- android user 版本如何默认adb调试为打开【转】
本文转载自:http://blog.csdn.net/chaihuasong/article/details/50342119 A. 软件准备 user版本需要先打开USB debug开关,打开方式如 ...
- linux内核的三种主要调度策略
linux内核的三种主要调度策略: 1,SCHED_OTHER 分时调度策略, 2,SCHED_FIFO实时调度策略,先到先服务 3,SCHED_RR实时调度策略,时间片轮转 实时进程将得到优先调用, ...
- POJ2914 Minimum Cut —— 最小割
题目链接:http://poj.org/problem?id=2914 Minimum Cut Time Limit: 10000MS Memory Limit: 65536K Total Sub ...
- lucene Index Store TermVector 说明
最新的lucene 3.0的field是这样的: Field options for indexingIndex.ANALYZED – use the analyzer to break the Fi ...
- [原创]java实现word转pdf
最近遇到一个项目需要把word 转成pdf,百度了一下网上的方案有很多,比如虚拟打印.给word 装扩展插件等,这些方案都依赖于ms word 程序,在java代码中也得使用诸如jacob或jcom这 ...