国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

496. Next Greater Element I

2019-11-08 02:56:38
字體:
供稿:網(wǎng)友

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.

class Solution {public: vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) { map<int, int> itemIdx; int sz = nums.size(); vector<int> ans; for(int i = 0; i < sz; ++i) itemIdx[nums[i]] = i; for(int i = 0; i < findNums.size(); ++i){ int idx = itemIdx[findNums[i]]; int j = idx + 1; for( ; j < sz; ++j){ if(nums[j] > nums[idx]){ ans.push_back(nums[j]); break; } } if(j == sz) ans.push_back(-1); } return ans; }};
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 利川市| 石景山区| 吉安市| 蚌埠市| 榆林市| 依兰县| 扶绥县| 永顺县| 璧山县| 华宁县| 纳雍县| 马尔康县| 丰城市| 道孚县| 无为县| 化德县| 防城港市| 无极县| 寿阳县| 济宁市| 马关县| 石柱| 和平区| 清水县| 香港 | 内乡县| 阳高县| 杨浦区| 莲花县| 喀喇沁旗| 民权县| 晋城| 哈密市| 镇安县| 齐河县| 高阳县| 固安县| 和田县| 崇文区| 望奎县| 承德市|