想到了用哈希表存这个数的位置,但是没有想到可以直接用哈希表存next great,用栈存还没找到的数,没遍历一个数就考察栈中的元素小,小的话,这个数就是栈中数的next great,栈中的数肯定是下大上小。 publicint[] nextGreaterElement(int[] nums1,int[] nums2) {/*通过map建立当前元素和其next great的映射 ...
1classSolution {2public:3vector<int> secondGreaterElement(vector<int>&nums) {4intn =nums.size();5vector<int>p;6for(inti =0; i < n; i++) {7p.push_back(i);8}9sort(p.begin(), p.end(), [&](inta,intb) {10returnnums[a] >nums[b];11});1213set<int>st;14st.insert(n),...
LeetCode题目:503. Next Greater Element II Given acircular array (the next element of the last element is the first element of the array),print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order next in the...
503. Next Greater Element II # 题目 # Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to
code: ``` class Solution(object): def nextGreaterElement(self, findNums, nums): """ :type findNums: List[int] :type nums: List[int] :rtype: List[int] """ dic = {} s = [] i = 0 while i != len(nums): if not s or s[-1] > nums[i]: ...
这个题和之前的503. Next Greater Element II做法一样的,都是使用一个单调递减栈保存每个数字的下标。 首先,把链表遍历一遍,转化成数组问题。 然后遍历数组,需要维护单调递减栈,每个元素的位置都要往栈里面放,放之前先把栈里面小于该元素的全部弹出。
[leetcode] 503. Next Greater Element II Description Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order...
下面将使用两种方法来解题,一种是暴力破解法,一种是Next Greater Number问题解法。 2.1、暴力破解法 从头开始遍历nums1,每次遍历从nums2中找到对应元素,然后从该元素的下一个元素开始依次比较,找出大于该值的第一个元素即可。 【代码实现】 class Solution {public int[] nextGreaterElement(int[] nums1, int[]...
简介:LeetCode之Next Greater Element I 1、题目 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...
leetcode503. Next Greater Element II 题目要求 Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order next ...