给定两个没有重复元素的数组 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的更多相关文章

  1. 503 Next Greater Element II 下一个更大元素 II

    给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 x 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它 ...

  2. [leetcode]496. Next Greater Element I下一个较大元素

    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...

  3. [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 ...

  4. Leetcode496.Next Greater Element I下一个更大的元素1

    给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的下一个比其大的值. nums1 中数字 x 的下一个更 ...

  5. 556. Next Greater Element III下一个更大的数字

    [抄题]: Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exac ...

  6. [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 ...

  7. [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 ...

  8. LeetCode 496. 下一个更大元素 I(Next Greater Element I) 35

    496. 下一个更大元素 I 496. Next Greater Element I 题目描述 给定两个没有重复元素的数组 nums1 和 nums2,其中 nums1 是 nums2 的子集.找到  ...

  9. LeetCode 556. 下一个更大元素 III(Next Greater Element III)

    556. 下一个更大元素 III 556. Next Greater Element III 题目描述 给定一个 32 位正整数 n,你需要找到最小的 32 位整数,其与 n 中存在的位数完全相同,并 ...

随机推荐

  1. 2016/05/25 PHP mysql_insert_id() 函数 返回上一步 INSERT 操作产生的 ID

    定义和用法 mysql_insert_id() 函数返回上一步 INSERT 操作产生的 ID. 如果上一查询没有产生 AUTO_INCREMENT 的 ID,则 mysql_insert_id() ...

  2. python day- 6 is 和 ==的区别 encode 和 decode

    1.is 和  == 的区别. == 是由来判断左右两边的内容是否相等. is 是用来判断内存地址是否相同. 引进 id (   )函数 小数据池: 对于字符串 ,数字 ,bool 值进行 id()计 ...

  3. var let Hositing const Temporal Dead Zone

    var  let  Hositing const Temporal Dead Zone 临时死区

  4. 怎么显示隐藏Mac上的隐藏文件

    打开终端,输入:defaults write com.apple.finder AppleShowAllFiles -bool true 此命令显示隐藏文件defaults write com.app ...

  5. Configuring Your EMS Server or EMS Console Server on Windows/Linux

    EMS Configuration Files RAD Studio provides the scripts to render the web-browser console, the EMS s ...

  6. [RK3288][Android6.0] 调试笔记 --- 通用GPIO驱动控制LED【转】

    本文转载自:http://m.blog.csdn.net/kris_fei/article/details/69553422 Platform: ROCKCHIPOS: Android 6.0Kern ...

  7. android之View坐标系(view获取自身坐标的方法和点击事件中坐标的获取)

    在做一个view背景特效的时候被坐标的各个获取方法搞晕了,几篇抄来抄去的博客也没弄很清楚. 现在把整个总结一下. 其实只要把下面这张图看明白就没问题了. 涉及到的方法一共有下面几个: view获取自身 ...

  8. python二维数组初始化

    >>> a=[[0]*3 for i in range(3)]>>> a[[0, 0, 0], [0, 0, 0], [0, 0, 0]]>>> ...

  9. 设置linux服务器下开放端口

    查询 netstat -anp  所有开放端口信息 二.关闭端口号: iptables -A OUTPUT -p tcp --dport 端口号-j DROP 三.打开端口号: iptables -A ...

  10. 「SHOI2007」「Codevs2341」 善意的投票(最小割

    2341 善意的投票 2007年省队选拔赛上海市队选拔赛 时间限制: 5 s 空间限制: 128000 KB 题目等级 : 大师 Master   题目描述 Description 幼儿园里有n个小朋 ...