Skip to content

Commit ac85dbe

Browse files
committed
-
1 parent 5722158 commit ac85dbe

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
503. Next Greater Element II
2+
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 in the array, which means you could search circularly to find its next greater number. If it doesn't exist, output -1 for this number.
3+
4+
Example 1:
5+
Input: [1,2,1]
6+
Output: [2,-1,2]
7+
Explanation: The first 1's next greater number is 2;
8+
The number 2 can't find next greater number;
9+
The second 1's next greater number needs to search circularly, which is also 2.
10+
11+
题目大意:给一个循环数组,返回一个等长的数组,数组中的每一个元素是:它后面的第一个大于它的元素(如果后面没有就循环一遍到最前面找,直到循环了一圈为止),如果不存在这样的数,就返回-1
12+
分析:首先建立一个等长的result数组,起始都是-1。和Next Greater Element I类似,不过这个题目要两个循环解决,第一个循环i从0~n-1,对于每一个nums[i],把他们的下标index都放入栈中。但是在放入栈之前需要做这样的事情:比较栈顶元素和nums[i],如果恰好比nums[i]小,说明nums[i]就是它的第一大元素,就将result[s.top()]的值变为nums[i],这样栈中的下标始终是它的后面找不到比他大的元素的下标,也就是说栈中在遍历一遍后剩下的元素的值都是递减次序的~
13+
开始第二次循环,依旧i从0~n-1,但是这次不需要push入栈了,只需要处理栈中剩下的元素,对于nums[i],如果栈顶元素和nums[i]比较,恰好nums[i]大,说明nums[i]就是他们这些没在后面找到最大元素的最大元素,出栈,result[s.top()] = nums[i]。这样所有遍历完毕后栈中只会剩下唯一一个元素,也就是该数组中最大的元素,它的result依旧是-1,其他的都已经更新完毕。也就是说,第一次遍历是为了找它后面比它大的元素,而第二次遍历是为了找前面比他大的元素~
14+
15+
class Solution {
16+
public:
17+
vector<int> nextGreaterElements(vector<int>& nums) {
18+
int n = nums.size();
19+
vector<int> result(n, -1);
20+
stack<int> s;
21+
for (int i = 0; i < n; i++) {
22+
while (!s.empty() && nums[s.top()] < nums[i]) {
23+
result[s.top()] = nums[i];
24+
s.pop();
25+
}
26+
s.push(i);
27+
}
28+
for (int i = 0; i < n; i++) {
29+
while (!s.empty() && nums[s.top()] < nums[i]) {
30+
result[s.top()] = nums[i];
31+
s.pop();
32+
}
33+
}
34+
return result;
35+
}
36+
};

0 commit comments

Comments
 (0)