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 ...
随机推荐
- Python学习笔记 变量
蒟蒻高举横幅:部分内容转自廖雪峰的Python教学 1.Python是动态语言,即它的变量是没有类型的. !/usr/bin/env python a = 'ABC' print a a = 123 ...
- ShoneSharp语言(S#)的设计和使用介绍系列(4)— 入门概述
ShoneSharp语言(S#)的设计和使用介绍 系列(4)- 入门概述 作者:Shone 声明:原创文章欢迎转载,但请注明出处,https://www.cnblogs.com/ShoneSharp. ...
- HDU5804--Price List
Price List Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others) Tot ...
- POJ1837--二维背包
Balance Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 13525 Accepted: 8474 Description ...
- 微信JS-SDK选择相册或拍照并上传PHP实现
理解:微信上传接口是拍照,或者选择本地照片,上传到微信的服务器,获取到一个id,通过token与这个id获取到图片,保存到服务器即可. 效果 通过微信js接口,调用底层程序. 需要引入js文件,并进行 ...
- MySQL 性能优化的最佳20多条经验分享(一)(转)
当我们去设计数据库表结构,对操作数据库时(尤其是查表时的SQL语句),我们都需要注意数据操作的性能.这里,我们不会讲过多的SQL语句的优化,而只是针对MySQL这一Web应用最多的数据库.希望下面的这 ...
- 【Flink】流-表概念
title: Flink流-表概念 date: 2017-12-12 14:48:16 categories: technique tags: Flink Flink Streaming Dynami ...
- 关于PHP新手学习的一些指导与建议,新手快到我碗里来!
新手小白想要系统性学好PHP开发,首先需要了解需要学些什么,然后给自己定下来一个学习路线,然后就朝着这个路线奋斗吧! 关于学习路线:(1) 熟悉HTML/CSS/JS等网页基本元素,完成阶段可自行制作 ...
- CentOS6 图形界面(gnome)安装,使用vnc进行远程连接
CentOS6相对于CentOS5的安装有了不少的进步,有不少默认的选项可以选择,如: Desktop :基本的桌面系统,包括常用的桌面软件,如文档查看工具. Minimal Desktop :基本的 ...
- Scala入门系列(十二):隐式转换
引言 Scala提供的隐式转换和隐式参数功能,是非常有特色的功能.是Java等编程语言所没有的功能.它可以允许你手动指定,将某种类型的对象转换成其他类型的对象.通过这些功能可以实现非常强大而且特殊的功 ...