496. Next Greater Element I
来源:互联网 发布:淘宝卖蚕丝被 编辑:程序博客网 时间:2024/05/19 10:10
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.
这道题主要就是通过循环先在nums数组里面找到对应的值,然后再在这个位置的后面找一找比它大的值,如果找不到就返回-1,还是比较直接的一个思路。同时用一个新的vector去存储数据
代码如下:
class Solution {public: vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) { vector<int>result; for(int i=0;i<findNums.size();i++){ bool flag=false; for(int j=0;j<nums.size();j++){ if(j==nums.size()-1){ break; } if(findNums[i]==nums[j]){ int m=j+1; while(m<=nums.size()-1) { if(nums[m]>findNums[i]){ result.push_back(nums[m]); flag=true; break; } m++; } } } if(!flag){ result.push_back(-1); } } return result; }};
阅读全文
0 0
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496.Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 约束满足问题的介绍
- SQL
- tf.cast用法
- hadoop上的工作流调度——oozie
- 1010 过河卒
- 496. Next Greater Element I
- 学习淘淘商城第六十七课(freemarker入门)
- 搜索引擎:倒排索引
- LeetCode 309. Best Time to Buy and Sell Stock with Cooldown
- SAP HANA LEFT/RIGHT字符串截取
- tf.convert_to_tensor的用法
- Java面向对象 (接口)
- java 后台Jsonp处理方法
- class文件结构