LeetCode-496 Next Greater Element I Solution(with Java)

1. Description:

Notes:

2. Examples:

3.Solutions:

 1 /**
 2  * Created by sheepcore on 2018-12-24
 3  */
 4 class Solution {
 5     public int[] nextGreaterElement(int[] nums1, int[] nums2) {
 6         int l1 = nums1.length, l2 = nums2.length;
 7         int[] res = new int[l1];
 8         for(int i  = 0; i < l1; i++){
 9             for(int j = 0; j < l2; j++){
10                 if(nums2[j] != nums1[i])  //locate the number in nums2
11                     continue;
12                 else{
13                     int k;
14                     for(k = j + 1; k < l2; k++){
15                         if(nums2[k] > nums1[i]) { //find the next greater one
16                             res[i] = nums2[k];
17                             break;
18                         }
19                     }
20                     if(k == l2) //don't find the next greater one
21                         res[i] = -1;
22                     break;
23 
24                 }
25             }
26         }
27         return res;
28     }
29 }

 

猜你喜欢

转载自www.cnblogs.com/sheepcore/p/12396580.html
今日推荐