496 The next bigger element I
Given two There are no duplicate elements Array of nums1 and nums2 , among nums1 yes nums2 Subset of . find nums1 Each element in the nums2
The next value in is larger than it .

nums1 Middle digit x The next larger element of is x stay nums2 The first ratio on the right of the corresponding position in x Big element . If it doesn't exist , Corresponding position output -1 .

Examples 1:

input :
nums1 = [4,1,2], nums2 = [1,3,4,2].
output :
[-1,3,-1]
explain :

about num1 Numbers in 4, You can't find the next larger number in the second array , So output -1.
about num1 Numbers in 1, The number in the second array 1 The next larger number on the right is 3.
about num1 Numbers in 2, There is no next larger number in the second array , So output -1.

Examples 2:

input :
nums1 = [2,4], nums2 = [1,2,3,4].
output :
[3,-1]
explain :

about num1 Numbers in 2 , The next large number in the second array is 3 .
about num1 Numbers in 4 , There is no next larger number in the second array , So output -1 .

Tips :

* nums1 and nums2 All elements in are unique .
* nums1 and nums2 The size of the array does not exceed 1000.
thinking :
use find The method is directly in the nums2 Found in nums1 The corresponding number in ( because nums1 yes nums2
Subset of , So I'm sure I can find it ), Then find the next number bigger than him .
code :
class Solution { public: vector<int> nextGreaterElement(vector<int> &nums1,
vector<int> &nums2) { vector<int> ans; for (int i = 0; i < nums1.size(); i++) {
auto iter = find(nums2.begin(), nums2.end(), nums1[i]); for (iter = iter + 1;
iter!= nums2.end(); iter++) { if (*iter > nums1[i]) { ans.push_back(*iter);
break; } } if (iter == nums2.end()) ans.push_back(-1); } return ans; } };

Technology
©2019-2020 Toolsou All rights reserved,
tkinter Implementation of user login interface Vue Invalid render dynamic component in subcomponent ( First invalid , Valid for the second time )9 ride 9 Sudoku C/C++ Memory model TypeScript actual combat -12-TS The mechanism of type checking - Type inference Java Thread safety and insecurity JLink,STLink,DAPLink,CMSIS DAP Differences in use Chapter 10 use SpringMVC Frame transformation of supermarket order management system -1C Language calculation of deposit interest About the Blue Bridge Cup , Things you should know !