LEETCODE--Next Greater Element I
来源:互联网 发布:洛桑联邦理工学院 知m 编辑:程序博客网 时间:2024/04/29 19:17
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: int find(int val, vector<int>& nums){ for(int j = 0; j < nums.size(); j++){ if(nums[j] == val) return j; } } vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) { vector<int> vec; for(int i = 0; i < findNums.size(); i++){ int pos = find( findNums[i] , nums ) + 1; for(;pos < nums.size(); pos++){ if(nums[pos] > findNums[i]){ vec.push_back(nums[pos]); break; } } if(pos == nums.size()) vec.push_back(-1); } return vec; }};
方法二:使用unordered_map
class Solution {public: vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) { unordered_map<int, int> dict; for(int i = 0; i < nums.size(); i++){ dict.insert( pair<int, int>( nums[i], i)); } vector<int> vec; for(int j = 0; j < findNums.size(); j++){ int pos = dict[findNums[j]] + 1; for(;pos < nums.size(); pos++){ if(nums[pos] > findNums[j]){ vec.push_back(nums[pos]); break; } } if(pos == nums.size()) vec.push_back(-1); } return vec; }};
- LEETCODE--Next Greater Element I
- [LeetCode] Next Greater Element I
- LeetCode:Next Greater Element I
- 【leetcode】Next Greater Element I
- LeetCode Next Greater Element I
- Next Greater Element I(leetcode)
- LeetCode-Next Greater Element I
- 【Leetcode】496. Next Greater Element I
- leetcode-496-Next Greater Element I
- leetcode -- 496. Next Greater Element I 【栈】
- 【LeetCode】496. Next Greater Element I
- LeetCode 496. Next Greater Element I
- LeetCode 496. Next Greater Element I
- Leetcode-Algorithms Next Greater Element I
- Leetcode #496 Next Greater Element I
- leetcode 496. Next Greater Element I
- LeetCode-496. Next Greater Element I
- [LeetCode]496. Next Greater Element I
- viewPager使用二
- 会话跟踪技术之HttpSession
- 深度学习caffe:激活函数
- 如何让mysql数据库允许远程访问
- POJ 3765 Xiang Hex G++
- LEETCODE--Next Greater Element I
- c语言des加密算法实现
- PAT_A 1022. Digital Library (30)
- 阿里百川
- 如何查网站的外链与索引量?
- jquery获取URL中参数解决中文乱码问题的两种方法
- jpa使用
- 使用testng+xml编写、执行自动化测试用例
- Android导出aar时嵌套引用的那些坑