LeetCode: 496 Next Greater Element I(easy)
题目:
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:
- All elements in
nums1
andnums2
are unique. - The length of both
nums1
andnums2
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)的更多相关文章
- [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 ...
- [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 解题报告
题目要求 You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset ...
- [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 ...
- [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 ...
- 496. Next Greater Element I - LeetCode
Question 496. Next Greater Element I Solution 题目大意:给你一个组数A里面每个元素都不相同.再给你一个数组B,元素是A的子集,问对于B中的每个元素,在A数 ...
- 496. Next Greater Element I 另一个数组中对应的更大元素
[抄题]: You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subse ...
- 【LeetCode】496. Next Greater Element I 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接遍历查找 字典保存位置 日期 题目地址:http ...
随机推荐
- [ExtJS5学习笔记]第五节 使用fontawesome给你的extjs5应用添加字体图标
本文地址:http://blog.csdn.net/sushengmiyan/article/details/38458411本文作者:sushengmiyan-------------------- ...
- java设计模式之综述
一.什么是设计模式 设计模式是一套被反复使用的.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了重用代码.让代码更容易被他人理解.保证代码可靠性. 毫无疑问,设计模式于己于他人于系 ...
- BZOJ3627: [JLOI2014]路径规划
BZOJ3627: [JLOI2014]路径规划 Description 相信大家都用过地图上的路径规划功能,只要输入起点终点就能找出一条最优路线.现在告诉你一张地图的信息,请你找出最优路径(即最短路 ...
- BZOJ 2069 POI2004 ZAW 堆优化Dijkstra
题目大意:给定一张无向图.每条边从两个方向走各有一个权值,求从点1往出走至少一步之后回到点1且不经过一条边多次的最短路 显然我们须要从点1出发走到某个和点1相邻的点上,然后沿最短路走到还有一个和点1相 ...
- SQL 关联操作
- UIVisualEffectView
UIBlurEffect 只支持到iOS 8.0+.系统给予的一个自动生成滤镜的方法 UIVisualEffectView *effectView = [[UIVisualEffectView all ...
- user版本如何永久性开启adb 的root权限【转】
本文转载自:http://blog.csdn.net/o0daxu0o/article/details/52933926 [Solution]* adb 的root 权限是在system/core/a ...
- Nginx安装教程(Centos6.8)
1.安装gcc gcc-c++(如新环境,未安装请先安装 yum install -y gcc gcc-c++ 2.安装wget yum -y install wget 3.安装PCRE库 cd /h ...
- hadoop 添加,删除节点
http://www.cnblogs.com/tommyli/p/3418273.html
- js 分享代码--完整示例代码
<div class="bdsharebuttonbox" data-tag="share_1"> <a class="bds_ms ...