给定两个没有重复元素的数组 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. ios 关于动画用法的总结

      #import "FirstVC.h" @implementation FirstVC /*     创建xib过程     1 创建xib(名字和类名相同)     2 文件 ...

  2. YTU 1055: 输入字符串以及输出

    1055: 输入字符串以及输出 时间限制: 1 Sec  内存限制: 128 MB 提交: 694  解决: 476 题目描述 编写一函数,由实参传来一个字符串,统计此字符串中字母.数字.空格和其它字 ...

  3. javascript XMLHttpRequest 对象的open() 方法参数说明

    下文是从w3c上摘录下来的,其中参数 method 说明的很简短,不是很理解,所以又找了些资料作为补充.文中带括号部分. XMLHttpRequest.open() 初始化 HTTP 请求参数 语法o ...

  4. codeforces 436A. Feed with Candy 解题报告

    题目链接:http://codeforces.com/contest/436/problem/A 题目意思:给出 n 颗只有两种类型:fruit 和 caramel的candies,这些candies ...

  5. SPOJ:K-Query Online(归并树)

    Given a sequence of n numbers a1, a2, ..., an and a number of k-queries. A k-query is a triple (i, j ...

  6. [Selenium] Android 中旋转屏幕,触摸,滚动

    package com.learingselenium.android; import junit.framework.TestCase import org.openqa.selenium.Rota ...

  7. [Selenium] 测试机器上安装了多个Firefox,如何指定运行哪一个?

    可通过FirefoxBinary 来指定运行某个路径下的Firefox, 示例代码如下: public class testFirefoxBinary{ public static void main ...

  8. BZOJ_2196_[Usaco2011 Mar]Brownie Slicing_二分答案+贪心

    BZOJ_2196_[Usaco2011 Mar]Brownie Slicing_二分答案+贪心 Description Bessie烘焙了一块巧克力蛋糕.这块蛋糕是由R*C(1 <= R,C ...

  9. bzoj 4756 Promotion Counting —— 线段树合并

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756 合并子树的权值线段树: merge 返回 int 或者是 void 都可以. 代码如下 ...

  10. DC 兼容的DC

    DC是 "Device Content" , MS VC++ 的 MFC图形设备接口 的 设备描述表.它是MFC的主要对象之一.通过CDC类进行各种绘图操作,例如选笔,选色,选涂色 ...