国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 原平市| 东城区| 西藏| 乌兰县| 安岳县| 上高县| 岳普湖县| 潮安县| 花莲市| 辽阳市| 龙岩市| 哈尔滨市| 涡阳县| 即墨市| 吉林省| 房产| 资中县| 海盐县| 墨玉县| 中方县| 建昌县| 临江市| 康定县| 东兰县| 荃湾区| 拜泉县| 平舆县| 赣州市| 公安县| 万宁市| 青州市| 兰西县| 咸阳市| 铜川市| 武宣县| 洛扎县| 峨山| 余江县| 张北县| 浦东新区| 黎平县|