-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNext_Greater_Element_I.java
More file actions
62 lines (55 loc) · 1.62 KB
/
Next_Greater_Element_I.java
File metadata and controls
62 lines (55 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.leetcode;
/*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.
*/
public class Next_Greater_Element_I {
public int[] nextGreaterElement(int[] findNums, int[] nums) {
//1.遍历findNums,记录当前元素 curNum
//2.nums 找==curNum ---findNum
//3.记录findNum的 记录index ,(index ~ length-1)遍历找到nums 下一个大数
int curNum = -1;
int[] result = new int[findNums.length];
int index = -1;
for (int i = 0; i < findNums.length; i++) {
result[i] = -1;
curNum = findNums[i];
index = findIndex(nums, curNum);
if(index >= 0){
int k = index+1;
for (; k < nums.length; k++) {
if(curNum < nums[k] ){
result[i] = nums[k];
break;
}else{
result[i] = -1;
}
}
}
}
///
return result;
}
private int findIndex(int[] nums, int curNum) {
int index = -1;
for (int j = 0; j < nums.length; j++) {
if (curNum == nums[j]) {
index =j;
break;
}
}
return index;
}
public static void main(String[] args) {
Next_Greater_Element_I s = new Next_Greater_Element_I();
int[] findNums ={2,4} ;
int[] nums ={1,2,3,4};
int[] result = s.nextGreaterElement(findNums, nums);
for (int n : result) {
System.out.print(n+" |");
}
}
}