您的位置:首页 > 其它

496. Next Greater Element I

2017-03-27 14:02 399 查看
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:

All elements in 
nums1
 and 
nums2
 are
unique.

The length of both 
nums1
 and 
nums2
 would
not exceed 1000.

答案
int search(vector<int> a,int b)

{
for(int i=0;i<a.size();i++)
{
if(a[i]==b)
return i;
}

}

class Solution {

public:

    vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {

        vector<int> ans;
for (int i = 0;i < findNums.size();i++)
{
int add;
add=search(nums,findNums[i]);

int j;

for(j=add;j<nums.size();j++)
{
if(findNums[i]<nums[j])
{
ans.push_back(nums[j]);
break;

}

}
if(j==nums.size())
ans.push_back(-1);
}
return ans; 

    }

};

本题较为简单,不用太复杂的想法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: