496. Next Greater Element I
You are given two arrays(without duplicates)nums1
andnums2
wherenums1
’s elements are subset ofnums2
. Find all the next greater numbers fornums1
's elements in the corresponding places ofnums2
.
The Next Greater Number of a numberxinnums1
is the first greater number to its right innums2
. 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.
题目的意思是nums1是nums2的子集,求nums1中的每一个元素在nums2中对应位置的右边第一个比它大的元素。没有则为-1,这一题读了半个小时没读懂=。=
class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
int result[] = new int[nums1.length]; for(int i = 0; i <nums1.length; i++)
{
int tmp = Integer.MAX_VALUE;
for(int j = 0; j<nums2.length; j++)
{
if(nums1[i] == nums2[j])
tmp = nums1[i];
if(nums2[j] > tmp)
{
result[i] = nums2[j];
break;
} }
}
for(int i = 0;i < result.length;i++)
if(result[i] == 0)result[i] = -1;
return result;
}
}
496. Next Greater Element I的更多相关文章
- 496. Next Greater Element I - LeetCode
Question 496. Next Greater Element I Solution 题目大意:给你一个组数A里面每个元素都不相同.再给你一个数组B,元素是A的子集,问对于B中的每个元素,在A数 ...
- [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 下一个较大的元素 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&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 ...
- 496. Next Greater Element I + 503. Next Greater Element II + 556. Next Greater Element III
▶ 给定一个数组与它的一个子列,对于数组中的一个元素,定义它右边第一个比他大的元素称为他的后继,求所给子列的后继构成的数组 ▶ 第 496 题,规定数组最后一个元素即数组最大元素的后继均为 -1 ● ...
- LeetCode: 496 Next Greater Element I(easy)
题目: You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset ...
随机推荐
- Shell脚本数据备份
- bootstrap 组件之"导航条"
一个典型的导航条基本代码如下: <nav class="navbar navbar-default"> <div class="container&qu ...
- C语言之二分猜数字游戏
#include <stdio.h>#include <windows.h>#include<string.h>int main() { int oldprice, ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
- Mybatis(四)关联映射
一. Mybatis关联映射 1 讲数据库中关联关系,主键表与外键表 一对多,多对一,主键表表示一 与外键表表示多 学生与学校的关系,工人与工厂,人员与部门的关系,.... 多 一 ...
- 缓存与ABP Redis Cache
缓存与ABP Redis Cache 为什么要用缓存 为什么要用缓存呢,说缓存之前先说使用缓存的优点. 减少寄宿服务器的往返调用(round-trips). 如果缓存在客户端或是代理,将减少对服务器的 ...
- php+jQuery+Mysql找回密码----ThinkPHP
最近用ThinkPHP做了一个邮箱找回密码功能,在遭遇了N个bug之后终于做成了,下面分享一下邮箱找回密码功能的实现: 邮箱找回密码实际上就是在用户通过验证之后重置密码的过程,一般开发者会在验证用户信 ...
- javascript中break,continue和return语句用法小结:
Break语句会使程序立刻退出包含在最底层的循环或者退出一个switch语句,它是用来退出循环或者switch语句. 例如: <script type="text/javascript ...
- FastDFS迁移步骤
1.在新的机器上安装FastDFS 2.将新的storage接到老的tracker机器上,用来同步数据(/usr/local/fastdfs/bin/fdfs_storaged) 数据同步完成后,需要 ...
- Java调用PDFBox打印自定义纸张PDF
打印对象 一份设置为A3纸张, 页面边距为(10, 10, 10, 10)mm的PDF文件. PageFormat 默认PDFPrintable无法设置页面大小. PDFPrintable print ...