Update 496. Next Greater Element I.md

This commit is contained in:
唐树森 2018-10-18 14:24:57 +08:00 committed by GitHub
parent 1836f71712
commit 3297cba31b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,8 +5,8 @@
* 1、若当前的元素n比s.top大就将栈顶pop出来(s.top的Next-Greater-Element就为n)循环直到元素n小于栈顶或者栈空 * 1、若当前的元素n比s.top大就将栈顶pop出来(s.top的Next-Greater-Element就为n)循环直到元素n小于栈顶或者栈空
* 2、将n进栈 * 2、将n进栈
所以栈里的元素肯定是自底向上递减的例如若nums=[1 6 5 3 4]则当遍历到3时栈s为[6,5]而3 < s.top() = 5所以直到此时依然没有找到5的Next-Greater-Element即5不应该pop出来遍历到4时s为[653] 所以栈里的元素肯定是自底向上递减的例如若nums=[1 6 5 3 4]则当遍历到3时栈s为[6,5]而3 < s.top() = 5所以直到此时依然没有找到5的Next-Greater-Element即5不应该pop出来遍历到4时s为[653]而4 > s.top() = 3, 所以3的Next-Greater-Element找到了应该将其pop出来然后4 < s.top() = 5所以5不应该pop
而4 > s.top() = 3, 所以3的Next-Greater-Element找到了应该将其pop出来然后4 < s.top() = 5所以5不应该pop 时间复杂度O(n)
# C++ # C++
``` ```
class Solution { class Solution {