国产探花免费观看_亚洲丰满少妇自慰呻吟_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;	}};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阿尔山市| 泰和县| 晴隆县| 钟山县| 通城县| 北川| 县级市| 永靖县| 诏安县| 盐源县| 加查县| 房产| 岫岩| 镇雄县| 分宜县| 江门市| 红安县| 牙克石市| 万安县| 个旧市| 沭阳县| 邢台市| 武汉市| 黄陵县| 丹凤县| 宁波市| 清苑县| 秀山| 东城区| 泾阳县| 宝鸡市| 临武县| 连城县| 尼木县| 凯里市| 武定县| 横山县| 西安市| 南雄市| 湖口县| 塘沽区|