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 ...
随机推荐
- ctfhub 双写绕过 文件头检查
双写绕过 进入环境 上传一句话木马 上传路径感觉不对检查源代码 从此处可以看出需要双写绕过 使用bp抓包 此处这样修改即可双写绕过 使用蚁剑连接 即可找到flag 文件头检查 进入环境 上传一句话木马 ...
- c的free 为什么不需要知道大小
malloc malloc函数在运行时分配内存.它需要以字节为单位的大小并在内存中分配那么多空间.这意味着malloc(50)将在内存中分配50个字节.它返回一个void指针 calloc 与mall ...
- .net大作业
登录页面: 源代码:当下较忙,后续会传至github上
- android.content.res.Resources$NotFoundException: String resource ID #0x0报错
报错:android.content.res.Resources$NotFoundException: String resource ID #0x0 原因:在setText()中使用了int型的参数 ...
- Wireshark-过滤器-数据包解析
目录 过滤器 数据包解析 参考 推荐阅读: https://www.cnblogs.com/zwtblog/tag/计算机网络/ 过滤器 显示过滤器 和 捕获过滤器,俩者使用非常类似. 在Wiresh ...
- Mybatis-Dao层实现(通过代理方式)
1.代理方式开发是主流 2.Mapper接口开发方法只需要编写Mapper接口(相当于Dao接口),然后由Mybatis根据接口创建动态代理对象 Mapper接口开发需要遵循以下规范 一一对应 Use ...
- Ubu18开机自启动
Ubu开机自启动 简单示例 在/etc/init.d/目录下新建启动脚本Test #!/bin/bash ### BEGIN INIT INFO # Provides: Test # Required ...
- 微信小程序循环列表点击每一个单独添加动画
首先,咱们看一下微信小程序动画怎么实现,我首先想到的是anime.js,但是引入之后用不了,微信小程序内的css也无法做到循环的动态,我就去找官方文档看看有没有相应的方法,哎,还真有 点击这里查看 微 ...
- canvas基础简单易懂教程(完结,多图)
目录 Canvas学习 一. Canvas概述 1.1 Hello world 1.2 Canvas的像素化 1.3 Canvas的动画思想 1.4 面向对象思维实现canvas动画 二.Canvas ...
- 2021.07.23 P3275 糖果(差分约束)
2021.07.23 P3275 糖果(差分约束) [P3275 SCOI2011]糖果 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 重点: 1.为了满足更多更多约束条件,合适地 ...