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

首頁 > 學院 > 開發設計 > 正文

[LeetCode] Next Greater Element I

2019-11-08 18:21:27
字體:
來源:轉載
供稿:網友

題目鏈接在此

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.大概就是找出表2中所有數字的 Next Greater Element :右側比他大的第一個數。表一相當于是個是query list,查詢單。用到棧和哈希。順便發現了個新大陸:for (int n : nums) C++ 11 可以這樣寫。(別鄙視我)
class Solution {public:	vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {		stack<int> s;		unordered_map<int, int> m;		for (int n : nums) {			while(!s.empty() && s.top() < n) {				m[s.top()] = n;  //  棧里面比n小的數,他們的Next Greater Element都是n				s.pop();			}			s.push(n);		}		vector<int> ans;		for (int n : findNums) {			ans.push_back(m.count(n) != 0 ? m[n] : -1);		}		return ans;	}};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 聂拉木县| 奇台县| 综艺| 门头沟区| 桃园市| 达拉特旗| 青阳县| 莎车县| 民县| 蓝山县| 临桂县| 库伦旗| 龙山县| 安吉县| 斗六市| 西贡区| 牙克石市| 托克逊县| 大安市| 太白县| 确山县| 靖江市| 嘉义市| 胶州市| 高清| 沁阳市| 三河市| 鸡泽县| 重庆市| 阿拉善右旗| 潼南县| 新竹市| 洛隆县| 堆龙德庆县| 乐东| 光山县| 安多县| 民和| 满洲里市| 芷江| 许昌市|