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. 意思就是给两个数组 nums1 = [4,1,2], nums2 = [1,3,4,2],数组nums1是数组nums2的子集,4在nums2数组是第三个,然后往他右边找,返回第一个比他大的值,若没有,返回-1
//暴力破解,结构不适用此题 #include <iostream>using namespace std;int main(){ bool d = true; bool e = true; int arr1[1001]; int arr2[1001]; int m, n; cin >> m >> n; for (int i = 0; i < m;i++) { cin >> arr1[i]; } for (int i = 0; i < n; i++) { cin >> arr2[i]; } for (int i = 0; i <m; i++) { d = true; e = true; for (int j = 0; j <n; j++) { if (arr1[i] == arr2[j]) { int m = j; while (m<n-1) { int max = arr2[j]; if (arr2[m + 1]>max) { cout << arr2[m + 1]; d = false; e = false; break; } m++; } while (d) { cout << -1; d = false; e = false; } while (!e) { break; } } } } return 0;}解法1: 网上大神的解决办法1
class Solution {public: vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) { vector<int> res(findNums.size());//这个当数组美滋滋啊 for (int i = 0; i < findNums.size(); ++i) { int j = 0, k = 0; for (; j < nums.size(); ++j) { if (nums[j] == findNums[i]) break; } for (k = j + 1; k < nums.size(); ++k) { if (nums[k] > nums[j]) { res[i] = nums[k]; break; } } if (k == nums.size()) res[i] = -1; } return res; }};观后感: 改进自己的://自己写的暴力破解,破解的也跟智障一样
#include <iostream>#include <vector>using namespace std;int main(){ int arr1[1001]; int arr2[1001]; int m, n; vector<int> res(10);//用来存储输出值 cin >> m >> n; for (int i = 0; i < m;i++) { cin >> arr1[i]; } for (int i = 0; i < n; i++) { cin >> arr2[i]; } for (int i = 0; i <m; i++) { int j=0,k=0; for (; j <n; j++) { if (arr1[i] == arr2[j]) { break; } } for (k = j + 1; k < n;k++) { if (arr2[k]>arr2[j]) { res[i] = arr2[k];//存储输出值 break; } } if (k==n) { res[i] = -1; } } for (int i = 0;i<m ;i++) { cout << res[i] << " "; } return 0;}解法2: 我们来对上面的方法稍做优化,我们用哈希表先来建立每个数字和其坐标位置之间的映射,那么我们在遍历子集合中的数字时,就能直接定位到该数字在原数组中的位置,然后再往右边遍历寻找较大数即可,参见代码如下:
class Solution {public: vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) { vector<int> res(findNums.size()); unordered_map<int, int> m;//建立哈希表,将值与索引建立联系 for (int i = 0; i < nums.size(); ++i) { m[nums[i]] = i;//例如将nums数组的值与索引建立关系,这个m数组不是一般的数组,自行画图理解 } for (int i = 0; i < findNums.size(); ++i) { res[i] = -1; int start = m[**findNums[i]**];//这个可以直接定位findNums中元素的值在nums中的位置 for (int j = start + 1; j < nums.size(); ++j) { if (nums[j] > findNums[i]) { res[i] = nums[j]; break; } } } return res; }};解法3: 下面这种方法使用了哈希表和栈,但是这里的哈希表和上面的不一样,这里是建立每个数字和其右边第一个较大数之间的映射,没有的话就是-1。我们遍历原数组中的所有数字,如果此时栈不为空,且栈顶元素小于当前数字,说明当前数字就是栈顶元素的右边第一个较大数,那么建立二者的映射,并且去除当前栈顶元素,最后将当前遍历到的数字压入栈。当所有数字都建立了映射,那么最后我们可以直接通过哈希表快速的找到子集合中数字的右边较大值,参见代码如下:
//暂时没看class Solution {public: vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) { vector<int> res; stack<int> st; unordered_map<int, int> m; for (int num : nums) { while (!st.empty() && st.top() < num) { m[st.top()] = num; st.pop(); } st.push(num); } for (int num : findNums) { res.push_back(m.count(num) ? m[num] : -1); } return res; }};参考资料: http://www.cnblogs.com/grandyang/p/6399855.html
新闻热点
疑难解答