You are given two arrays(without duplicates)nums1andnums2wherenums1’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 numberxinnums1is 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:

  1. All elements innums1andnums2are unique.
  2. The length of bothnums1andnums2would not exceed 1000.

题目的意思是nums1是nums2的子集,求nums1中的每一个元素在nums2中对应位置的右边第一个比它大的元素。没有则为-1,这一题读了半个小时没读懂=。=

例如Example1 中的1,在nums2中的右边元素为[3,4,2],第一个比1大的是3。
暴力遍历法:
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的更多相关文章

  1. 496. Next Greater Element I - LeetCode

    Question 496. Next Greater Element I Solution 题目大意:给你一个组数A里面每个元素都不相同.再给你一个数组B,元素是A的子集,问对于B中的每个元素,在A数 ...

  2. [leetcode]496. Next Greater Element I下一个较大元素

    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...

  3. 496. Next Greater Element I 另一个数组中对应的更大元素

    [抄题]: You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subse ...

  4. [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 ...

  5. LeetCode 496 Next Greater Element I 解题报告

    题目要求 You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset ...

  6. [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 ...

  7. [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 ...

  8. 496. Next Greater Element I + 503. Next Greater Element II + 556. Next Greater Element III

    ▶ 给定一个数组与它的一个子列,对于数组中的一个元素,定义它右边第一个比他大的元素称为他的后继,求所给子列的后继构成的数组 ▶ 第 496 题,规定数组最后一个元素即数组最大元素的后继均为 -1 ● ...

  9. LeetCode: 496 Next Greater Element I(easy)

    题目: You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset ...

随机推荐

  1. 秒表计时器以及Stopwatch

    Stopwatch:秒表计时器,用来记录程序的运行时间,通常用来测试代码在时间上的执行效率.(需要引用:System.Diagnostics.) Stopwatch sw=new Stopwatch( ...

  2. 多服务器操作利器 - Polysh

    多台服务器下的痛苦人生 分布式架构下的系统,可以说每个服务都是分别部署在多台服务器上的,有的甚至还需要多机房,在这种架构下可以说可以很好的做到了易扩展.容灾等功能.推荐的服务部署为一服务多机器.一机器 ...

  3. Tarjan算法:求解图的割点与桥(割边)

    简介: 割边和割点的定义仅限于无向图中.我们可以通过定义以蛮力方式求解出无向图的所有割点和割边,但这样的求解方式效率低.Tarjan提出了一种快速求解的方式,通过一次DFS就求解出图中所有的割点和割边 ...

  4. 【原创】python爬虫获取网站数据并存入本地数据库

    #coding=utf-8 import urllib import re import MySQLdb dbnumber = MySQLdb.connect('localhost', 'root', ...

  5. 【javaFX学习】(二) 面板手册--1

    找了好几个资料,没找到自己想要的,自己写个列表吧,方便以后用的时候挑选,边学边记.以学习笔记为主,所以会写的会偏个人记忆性.非教程,有什么问题一起讨论啊. 各个不同的控件放入不同的面板中有不同的效果, ...

  6. thinkphp 使用插件异步上传图片或者文件

    使用tp做一些上传的功能,的确挺方便.但是在一些特殊情况下无法单独的使用tp的上传功能, 或者需要做一些比较酷炫的上传效果,这里就需要用到框架了. 我在这里使用的是uploadify上传插件. 首先需 ...

  7. 学习makefile的一个工程示例

    前言 makefile推荐资料为陈皓的跟我一起写makefile,需要pdf资源的可以私我 正文 工程目录结构 ---include(放置头文件.h) ------student.h(Student类 ...

  8. 用python 抓取B站视频评论,制作词云

    python 作为爬虫利器,与其有很多强大的第三方库是分不开的,今天说的爬取B站的视频评论,其实重点在分析得到的评论化作嵌套的字典,在其中取出想要的内容.层层嵌套,眼花缭乱,分析时应细致!步骤分为以下 ...

  9. OpenStack搭建遇到的问题

    前言:对于像我这种新手来说,搭建OpenStack真的很费劲,因为我总是每配置一个服务,我就想弄懂,后来搭建过程很累人,因此我想了个办法,等我搭建出来再学.我这里将记录我从开始之初到我学习,再到我毕业 ...

  10. Scala中的override

    Scala中的override override是覆盖的意思,在很多语言中都有,在scala中,override是非常常见的,在类继承方面,它和java不一样,不是可写可不写的了,而是必须写的.如果不 ...