LeetCode——Next Greater Element I
LeetCode——Next Greater Element I
Question
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 and nums2 are unique.
The length of both nums1 and nums2 would not exceed 1000.
解题思路
想的就是过滤一遍第二个数组,把每个数右边比它大的第一个数存起来,然后遍历第一个数组,为每个元素找到第一个大的元素。
具体实现
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
map<int, int> dict;
for (int i = 0; i < nums.size(); i++) {
int flag = 0;
int j = i + 1;
for (; j < nums.size(); j++) {
if (nums[j] > nums[i]) {
flag = 1;
break;
}
}
if (flag) {
dict[nums[i]] = nums[j];
} else {
dict[nums[i]] = -1;
}
}
vector<int> res;
for (int i : findNums) {
res.push_back(dict[i]);
}
return res;
}
};
相关解答中,用到了栈来遍历第二个数组,这样的时间复杂度会降低到O(n),而以上这个算法的时间复杂度为O(n^2)。
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
stack<int> s;
unordered_map<int, int> m;
for (int n : nums) {
while (s.size() && s.top() < n) {
m[s.top()] = n;
s.pop();
}
s.push(n);
}
vector<int> ans;
for (int n : findNums) ans.push_back(m.count(n) ? m[n] : -1);
return ans;
}
};
LeetCode——Next Greater Element I的更多相关文章
- [LeetCode] Next Greater Element III 下一个较大的元素之三
Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...
- [LeetCode] Next Greater Element II 下一个较大的元素之二
Given a circular array (the next element of the last element is the first element of the array), pri ...
- [LeetCode] Next Greater Element I 下一个较大的元素之一
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...
- LeetCode Next Greater Element III
原题链接在这里:https://leetcode.com/problems/next-greater-element-iii/description/ 题目: Given a positive 32- ...
- LeetCode: Next Greater Element I
stack和map用好就行 public class Solution { public int[] nextGreaterElement(int[] findNums, int[] nums) { ...
- [leetcode]Next Greater Element
第一题:寻找子集合中每个元素在原集合中右边第一个比它大的数. 想到了用哈希表存这个数的位置,但是没有想到可以直接用哈希表存next great,用栈存还没找到的数,没遍历一个数就考察栈中的元素小,小的 ...
- [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] 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 ...
随机推荐
- Java RSA加密以及验签
签名加密以及验签工具类: 一般秘钥分为3个key 1.自己生成的私钥, 2.通过私钥生成的公钥1 3.通过提交公钥1给某宝,获取的公钥2. RSA公钥加密算法简介 非对称加密算法.只有短的RSA钥匙才 ...
- 网络虚拟化之FlowVisor:网络虚拟层(上)
概念解释:切片:虚拟网络的一个实例 一. 网络虚拟化(虚拟网络) 人类社会的发展在很大方面得益于自然界,飞机受益于鸟,雷达受益于蝙蝠等等,所以专门有个学科为仿生学就是研究和模仿生物的特殊本质,利用生物 ...
- 通用且常用的Java正则匹配工具,用以检查邮箱名、电话号码、用户密码、邮政编码等合法性
一个通用且常用的Java正则匹配工具,用以检查邮箱名.电话号码.用户密码.邮政编码等合法性. import java.util.regex.Matcher; import java.util.rege ...
- [LintCode] 带最小值操作的栈
class MinStack { public: MinStack() { // do initialization if necessary } void push(int number) { // ...
- DTD的学习和理解
看log4j的官方文档,上面说提供了XML格式的配置,但是没有XML具体示例.发现文档中说的是一个DTD文档,但我根本不知道DTD是什么,于是就简单了解一下.顺带做一下笔记. 注:结合笔记看log4j ...
- 解决----------“win10,不能打字了,已禁用IME”
登录Windows 10系统桌面后,右键点击左下角的开始菜单图标,然后在弹出菜单中选择“计算机管理”菜单项在打开的“计算机管理”窗口中,找到左侧系统工具下的“任务计划程序”菜单项点击任务计划程序前面的 ...
- SQL与Access使用查询结果集更新表
SQL语法 update 表1 set 字段1 = bb.字段1 from 表1 as aa, (select 字段1,字段2 from 表) bb where aa.字段2 = bb.字段2 Acc ...
- Redis配置文件的使用
Redis基本配置 常规配置 进到配置文件下 vi /etc/redis.conf 写入配置项 port 1111 # 配置端口号 daemonize yes # 是否后台运行 daemonize y ...
- mysql 中sum (if())与case
先来一个简单的sum select sum(qty) as total_qty from inventory_product group by product_id 这样就会统计出所有product的 ...
- Hibernate简单配置
1.配置构建路径,加载用户库,hibernate4.3.8 MySQL-Driver 2.写User.java 纯POJO+持久化注解=PO @Entity @Table(name=&quo ...