[leetcode]496. Next Greater Element I
Analysis
生而为人要善良啊~—— [ummmm~]
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.
给定两个数组,判断前面数组中的每个数在后面组数中的位置,然后找到该位置之后第一个大于这个数的数字,入梅没找到则为-1。
Implement
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
vector<int> res;
int len1 = findNums.size();
int len2 = nums.size();
for(int i=0; i<len1; i++){
int j;
for(j=0; j<len2; j++){
if(nums[j] == findNums[i])
break;
}
j += 1;
int find = 0;
while(j < len2){
if(nums[j] > findNums[i]){
res.push_back(nums[j]);
find = 1;
break;
}
j++;
}
if(find == 0)
res.push_back(-1);
}
return res;
}
};