题目:

You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.

The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number.

Example 1:

Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]
Explanation:
For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
For number 1 in the first array, the next greater number for it in the second array is 3.
For number 2 in the first array, there is no next greater number for it in the second array, so output -1.

Example 2:

Input: nums1 = [2,4], nums2 = [1,2,3,4].
Output: [3,-1]
Explanation:
For number 2 in the first array, the next greater number for it in the second array is 3.
For number 4 in the first array, there is no next greater number for it in the second array, so output -1.

Note:

  1. All elements in nums1 and nums2 are unique.
  2. The length of both nums1 and nums2 would not exceed 1000.

代码:

自己的:

 class Solution {
public:
vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
vector<int> result;
int s1 = findNums.size();
int s2 = nums.size();
bool b = ;
for (int i=; i<s1; i++){
int tem = -;
for (int j=; j <s2; j++){
if (findNums[i] == nums[j]){
for (int k=j; k<s2; k++){
if (nums[k]>findNums[i]){
tem = nums[k];
break;
}
}
}
}
result.push_back(tem);
}
return result;
}
};

别人的:

 class Solution {
public:
vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
stack<int> s;
unordered_map<int,int> hash;
for(int i=;i<nums.size();i++){
if(s.empty()){
s.push(nums[i]);
}
else if(nums[i] > s.top()){
while(!s.empty() && s.top()<nums[i]){
hash[s.top()] = nums[i];
s.pop();
}
s.push(nums[i]);
}
else s.push(nums[i]);
}
while(!s.empty()){
hash[s.top()] = -;
s.pop();
}
vector<int> res;
for(int i=;i<findNums.size();i++){
res.push_back(hash[findNums[i]]);
}
return res;
}
};

unordered_map类是c++11标准的内容,具体介绍见链接:https://msdn.microsoft.com/zh-cn/library/bb982522.aspx

LeetCode: 496 Next Greater Element I(easy)的更多相关文章

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

  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 解题报告

    题目要求 You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset ...

  4. [LeetCode] 496. Next Greater Element I_Easy tag: Stack

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

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

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

  7. 496. Next Greater Element I - LeetCode

    Question 496. Next Greater Element I Solution 题目大意:给你一个组数A里面每个元素都不相同.再给你一个数组B,元素是A的子集,问对于B中的每个元素,在A数 ...

  8. 496. Next Greater Element I 另一个数组中对应的更大元素

    [抄题]: You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subse ...

  9. 【LeetCode】496. Next Greater Element I 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接遍历查找 字典保存位置 日期 题目地址:http ...

随机推荐

  1. Centos7安装配置ansible运维自动化工具

    准备至少两台机器 Centos7,这两台机器都关闭 selinux IP:106.13.118.132 服务端(ansible) masterIP:148.70.60.244 节点 slaver 服务 ...

  2. 图像处理之opencv---加减乘除运算cvdiv

    http://chyyeng.blog.163.com/blog/static/16918230201211632135456/

  3. 记使用WaitGroup时的一个错误

    记使用WaitGroup时的一个错误 近期重构我之前写的server代码时,不当使用了WaitGroup,碰到了个错误,记录下. package main import ( "fmt&quo ...

  4. eclipse调试第三方jar包需要源码的问题

    很多时候测试自己的jar包功能时,需要有一个测试工程导入该jar包,但是一般在调试的时候,需要跟进去看看步骤和逻辑是否正确,这个时候就需要在jar包的源码中下断点.最近刚好自己也会经常这样做,也遇到了 ...

  5. RAC 单节点实例异常关闭,关键报错ORA--29770

    监控系统监控到RAC 的一个实例异常关闭 ,时间是凌晨1点多,还好没有影响到业务 之后就是分析原因 这套RAC搭建在虚拟化环境OS SUSE11 查看oracel alert log信息 Mon :: ...

  6. Appium基础——需要知道的

      Appium使用平台厂商提供的自动化框架: 1.ios 苹果的UIAutomation 2.android google的UIAutomator Appium底层使用厂商提供的自动化框架,就不需要 ...

  7. 安装Nginx四层负载均衡

    Nginx1.9开始支持tcp层的转发,通过stream实现的,而socket也是基于tcp通信. stream模块默认不安装的,需要手动添加参数:–with-stream,官方下载地址:downlo ...

  8. Java中锁的内存语义

    我们都知道,Java中的锁可以让临界区互斥执行.锁是Java并发编程中最重要的同步机制,锁除了可以让临界区互斥执行外,还可以让释放锁的线程向获取同一个锁的线程发送消息.下面是锁的释放-获取的代码: C ...

  9. ios非UTF-8格式的网页解析

    网上有很多关于ios xml解析的方法,关于非UTF-8格式的网页解析也不少,我也试着看了好几个,但都没成功.今天无意中却弄好了,所以想和大家分享下.其实很简单,下面说下怎么得到非UTF-8格式的网页 ...

  10. hdu-5726 GCD(rmq)

    题目链接: GCD Time Limit: 10000/5000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Prob ...