496. Next Greater Element I
2017-03-27 14:02
399 查看
You are given two arrays (without duplicates)
elements are subset of
elements in the corresponding places of
The Next Greater Number of a number x in
the first greater number to its right in
this number.
Example 1:
Example 2:
Note:
All elements in
unique.
The length of both
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;
}
};
本题较为简单,不用太复杂的想法
nums1and
nums2where
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
nums1is
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
nums1and
nums2are
unique.
The length of both
nums1and
nums2would
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;
}
};
本题较为简单,不用太复杂的想法
相关文章推荐
- 496. Next Greater Element I
- Leetcode算法学习日志-496 Next Greater Element
- 496. Next Greater Element I
- leetcode[496]:Next Greater Element I
- 496. Next Greater Element I LeetCode
- 496. Next Greater Element I
- 496. Next Greater Element I
- leetcode 496 Next Greater Element I
- [LeetCode] 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- leetcode_496 Next Greater Element I
- 【LeetCode】496 Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I