数组和矩阵(3)——Next Greater Element I

https://leetcode.com/problems/next-greater-element-i/#/description

You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1‘s elements in the corresponding places of nums2.

The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. 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.

1.暴力

 1 public class Solution {
 2     public int[] nextGreaterElement(int[] findNums, int[] nums) {
 3         int len = findNums.length;
 4         int[] res = new int[len];
 5         if(len > nums.length) {
 6             return res;
 7         }
 8         for(int i=0; i<len; i++) {
 9             boolean flag = false;
10             boolean k = false;
11             for(int j=0; j<nums.length; j++) {
12                 if(findNums[i] == nums[j]) {
13                     flag = true;
14                     continue;
15                 }
16                 if(flag == true && nums[j] > findNums[i]) {
17                     res[i] = nums[j];
18                     k = true;
19                     break;
20                 }
21             }
22             if(k == false) {
23                 res[i] = -1;
24             }
25         }
26         return res;
27     }
28 }

2.集合

 1     public int[] nextGreaterElement(int[] findNums, int[] nums) {
 2         Map<Integer, Integer> map = new HashMap<>(); // map from x to next greater element of x
 3         Stack<Integer> stack = new Stack<>();
 4         for (int num : nums) {
 5             while (!stack.isEmpty() && stack.peek() < num)
 6                 map.put(stack.pop(), num);
 7             stack.push(num);
 8         }
 9         for (int i = 0; i < findNums.length; i++)
10             findNums[i] = map.getOrDefault(findNums[i], -1);
11         return findNums;
12     }
时间: 2024-10-12 12:38:48

数组和矩阵(3)——Next Greater Element I的相关文章

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

[抄题]: You are given two arrays (without duplicates) nums1 and nums2 where nums1's elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2. The Next Greater Number of a number x in nums

Leetcode 503. Next Greater Element II JAVA语言

Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order next in the ar

[leetcode] Next Greater Element

今天处理一下一系列题目:Next Greater Element系列. Next Greater Element I You are given two arrays (without duplicates) nums1 and nums2 where nums1's elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of

leetcode496 - Next Greater Element I - easy &amp;&amp; leetcode503 - Next Greater Element II - medium

496. Next Greater Element IYou are given two arrays (without duplicates) nums1 and nums2 where nums1's elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.The Next Greater Number o

LeetCode 496. 下一个更大元素 I(Next Greater Element I) 35

496. 下一个更大元素 I 496. Next Greater Element I 题目描述 给定两个没有重复元素的数组?nums1 和?nums2,其中 nums1?是?nums2?的子集.找到?nums1?中每个元素在?nums2?中的下一个比其大的值. nums1?中数字?x?的下一个更大元素是指?x?在?nums2?中对应位置的右边的第一个比?x?大的元素.如果不存在,对应位置输出 -1. 每日一算法2019/6/7Day 35LeetCode496. Next Greater Ele

HDU - 2294 Pendant (DP滚动数组降维+矩阵快速幂)

Description On Saint Valentine's Day, Alex imagined to present a special pendant to his girl friend made by K kind of pearls. The pendant is actually a string of pearls, and its length is defined as the number of pearls in it. As is known to all, Ale

poj 1195:Mobile phones(二维树状数组,矩阵求和)

Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14489   Accepted: 6735 Description Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The

数组 array 矩阵 list 数据框 dataframe

转自 :  http://blog.csdn.net/u011253874/article/details/43115447 <span style="font-size:14px;">#R语言备忘录三# #数组array和矩阵matrix.列表list.数据框dataframe #数组 #数组的重要属性就是dim,维数 #得到4*5的矩阵 z <- 1:12 dim(z) <- c(3,4) z #构建数组 x <- array(1:20, dim = 

R读书笔记一:变量,向量,数组,矩阵,数据框,读写文件,控制流

R读书笔记一:变量,向量,数组,矩阵,数据框,读写文件,控制流 1.创建向量和矩阵 函数c( ), length( ), mode( ), rbind( ), cbind( ) 1)创建向量,求向量长度,向量类型. > x1=c(2,4,6,8,0) > x2=c(1,3,5,7,9) > length(x1) [1] 5 > mode(x1) [1] "numeric" > x1 [1] 2 4 6 8 0 > x1[3] [1] 6 > a