496. Next Greater Element I - LeetCode
Question

Solution
题目大意:给你一个组数A里面每个元素都不相同。再给你一个数组B,元素是A的子集,问对于B中的每个元素,在A数组中相同元素之后第一个比它的元素是多少。
思路:把nums1中的元素存储到一个map里,遍历nums2,如果能从map中取到值,就遍历nums2中后续元素的值并与当前元素做比较,如果存在比当前元素大的值就取该值(第一个),否则返回-1.
Java实现:
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
Map<Integer, Integer> map = new HashMap<>();
for (int tmp : nums1) {
map.put(tmp, -1);
}
for (int i = 0; i < nums2.length; i++) {
int tmp = nums2[i];
Integer nextGreaterElement = map.get(tmp);
if (nextGreaterElement != null) {
for (int j=i+1; j<nums2.length; j++) {
if (tmp < nums2[j]) {
nextGreaterElement = nums2[j];
break;
}
}
map.put(tmp, nextGreaterElement);
// System.out.println(tmp + ", " + nextGreaterElement);
}
}
// System.out.println();
int[] result = new int[nums1.length];
for (int i = 0; i < nums1.length; i++) {
result[i] = map.get(nums1[i]);
// System.out.print(result[i] + ",");
}
return result;
}
496. Next Greater Element I - LeetCode的更多相关文章
- [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 ...
- 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 ...
- 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&Python] Problem 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(easy)
题目: You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset ...
- 【leetcode】496. Next Greater Element I
原题 You are given two arrays (without duplicates) nums1 and nums2 where nums1's elements are subset o ...
随机推荐
- I2C总线完全版——I2C总线的结构、工作时序与模拟编程
I2C总线的结构.工作时序与模拟编程 I2C总线的结构.工作时序与模拟编程I2C总线(Inter Integrated Circuit)是飞利浦公司于上个世纪80年代开发的一种"电路板级&q ...
- C++ | 虚函数表内存布局
虚表指针 虚函数有个特点.存在虚函数的类会在类的数据成员中生成一个虚函数指针 vfptr,而vfptr 指向了一张表(简称,虚表).正是由于虚函数的这个特性,C++的多态才有了发生的可能. 其中虚函数 ...
- CSS系列——浏览器默认样式
了解HTML标签在各浏览器当中的默认样式,可以让我们了解,为什么会要写Reset.css,Reset.css当中要怎么写样式最合理.试着思考下面的问题: 为什么会有默认样式? 每个浏览器的默认样式有什 ...
- 单例设计模式(Singleton)
一.单例设计模式介绍 所谓类的单例设计模式,就是采取一定的方法保证在整个的软件系统中,对某个类只能存在一个对象实例, 并且该类只提供一个取得其对象实例的方法(静态方法) 例如:Hibernate的Se ...
- 动态修改svg的颜色,svg做背景色时候修改颜色
svg修改背景色可以使用fill属性来修改,但是我现在需要动态改变svg的颜色,例如我hover的时候 现在发现一种兼容性还不错的方法是css属性mask 类似于给路径填充上颜色,结合backgrou ...
- 搭建 LNMP 环境
搭建 LNMP 环境 搭建 Nginx 静态服务器 安装 Nginx 使用 yum 安装 Nginx: yum install nginx -y 修改 /etc/nginx/conf.d/defaul ...
- SVN 添加账号密码的方法(Windows 系统完整版)
前言: 本人新接了一个项目,目前该项目基本完工,现在想要将该项目上传至SVN上保管,然后设置并添加账号密码信息,以便于后期加入这个项目的小伙伴可以通过新增加的账号密码信息获取到SVN项目,以便后期项目 ...
- numpy---(上)
Numpy Numpy ndarray N维数组对象ndarray, 是一系列同类型数据的集合, 索引以0下标开始, 创建一个ndarray对象, 需调用array函数: numpy.array(ob ...
- Chrome JSON格式化插件
1.JSONView插件下载地址:https://github.com/gildas-lormeau/JSONView-for-Chrome 2.解压(E:\json) 3.打开Chrome-扩展程序 ...
- Intel主板芯片组
写这个的初衷还是由于linux内核本身就是硬件的抽象,如果你对硬件的相关发展,机制以及架构不了解,实际你也是看不懂linux内核代码以及看不懂linux很多命令输出的结果的,如果你看内核代码就会发现内 ...