leetcode 496. Next Greater Element I
来源:互联网 发布:mac 内存占用 编辑:程序博客网 时间:2024/06/05 02:52
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.
本题题意很简单,就是寻找在原数组中右边比自己大的元素,假如不存在返回-1,那么直接循环暴力求解即可
代码如下:
#include <iostream>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <climits>#include <algorithm>#include <sstream>#include <functional>#include <bitset>#include <cmath>using namespace std;class Solution {public: vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) { vector<int> res; for (int a : findNums) { int i = 0; for (i = 0; i < nums.size(); i++) { if (nums[i] == a) break; } for (i=i+1; i < nums.size(); i++) { if (nums[i] > a) break; } res.push_back(i >= nums.size() ? -1 : nums[i]); } return res; }};
- 【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-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】496. Next Greater Element I
- Leetcode 496. Next Greater Element I
- LeetCode:496. Next Greater Element I
- LeetCode OJ 496. Next Greater Element I
- 【C++】【LeetCode】496. Next Greater Element I
- LeetCode 496. Next Greater Element I
- vue,keep-alive的作用
- Pytorch--MNIST练习--clean版
- fdisk -l文件详解
- TensorFlow白皮书阅读笔记(含下载)
- kafka和flume的区别和对比使用
- leetcode 496. Next Greater Element I
- dtd声明和entityResolver避免saxReader联网验证
- C/C++读写文本文件、二进制文件
- HALCON的算子描述
- SpringMVC中使用Interceptor拦截器
- STM32中使用静态“字符串的方式”
- SpringBoot入门案例
- idea中出现中文乱码时,在jsp最上面加上
- 一体机安装win8系统后分区全部合并到一个盘其它分区的数据怎么寻回