码迷,mamicode.com
首页 > 其他好文 > 详细

[leetcode] Next Greater Element

时间:2018-07-13 23:43:36      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:span   提交   element   array   hashmap   duplicate   his   gre   比较   

今天处理一下一系列题目: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 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.

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 in nums1 and nums2 are unique.
  2. The length of both nums1 and nums2 would not exceed 1000.

问题一是基础,翻译一下是这样:两个数组nums1是nums2的子集,而且nums2中各个数字都不重复。然后要求找nums1中的数字在nums2中的位置以右,是否还有比这个数字更大的数,要是有保存下来,要是没有就是-1。

思路也比较清楚,第一个想法肯定是两次遍历,但是肯定是最差的想法,我也就没有写。第二个思路,因为提到nums2中每个数字不重复,很容易联想到map,所以这个思路就是用map记录nums2中每个元素的位置,然后nums1中的元素去找的时候起始点就比较明确了。代码如下:

 1 class Solution {
 2     public int[] nextGreaterElement(int[] nums1, int[] nums2) {
 3         int[] res = new int[nums1.length];
 4         Map<Integer,Integer> map = new HashMap<>();
 5         for ( int i = 0 ; i < nums2.length ; i ++ ) map.put(nums2[i],i);
 6         for ( int i = 0  ; i < nums1.length ; i ++ ){
 7             for ( int j = map.get(nums1[i]) ; j < nums2.length ; j ++ ){
 8                 if ( nums2[j] > nums1[i] ) {
 9                     res[i] = nums2[j];
10                     break;
11                 }else res[i] = -1;
12             }
13         }
14         return res;
15     }
16 }

      运行时间3ms,击败99.95%的提交。应该是现在能优化的最优的方法了。


Next Greater Element II

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 array, which means you could search circularly to find its next greater number. If it doesn‘t exist, output -1 for this number.

Example 1:

Input: [1,2,1]
Output: [2,-1,2]
Explanation: The first 1‘s next greater number is 2; 
The number 2 can‘t find next greater number;
The second 1‘s next greater number needs to search circularly, which is also 2.

Note: The length of given array won‘t exceed 10000.

现在题目变了一下,翻译一下:给一个数组nums,这个数组是一个循环数组,也就是最后一个元素的下一个元素是第一个元素。要求找数组中每个元素的next greater element(定义和上面题目一样)。

 第一个思路:根据循环数组的定义,我们可以再定义一个长度为nums.length*2的数组,然后问题就简单了。代码如下:

 1 class Solution {
 2    public int[] nextGreaterElements(int[] nums) {
 3         int[] array = new int[nums.length*2];
 4         int[] res = new int[nums.length];
 5         for ( int i = 0 ; i < nums.length ; i ++ ) {
 6             array[i] = nums[i];
 7             array[i+nums.length] = nums[i];
 8         }
 9         for ( int i = 0 ; i < nums.length ; i ++ ){
10                 for (int j = i; j < array.length; j++) {
11                     if (array[j] > nums[i]) {
12                         res[i] = array[j];
13                         break;
14                     } else{
15                         res[i] = -1;
16                     }
17                 }
18         }
19         return res;
20     }
21 }

      运行时间46ms,击败74.06%提交。


 

[leetcode] Next Greater Element

标签:span   提交   element   array   hashmap   duplicate   his   gre   比较   

原文地址:https://www.cnblogs.com/boris1221/p/9307917.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!