From 3297cba31b4f1a35af6e341ca2936c4a5d86a127 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E6=A0=91=E6=A3=AE?= <14021051@buaa.edu.cn> Date: Thu, 18 Oct 2018 14:24:57 +0800 Subject: [PATCH] Update 496. Next Greater Element I.md --- 496. Next Greater Element I.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/496. Next Greater Element I.md b/496. Next Greater Element I.md index 2572503..94569f8 100644 --- a/496. Next Greater Element I.md +++ b/496. Next Greater Element I.md @@ -5,8 +5,8 @@ * 1、若当前的元素n比s.top大就将栈顶pop出来(s.top的Next-Greater-Element就为n),循环直到元素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为[6,5,3], -而4 > s.top() = 3, 所以3的Next-Greater-Element找到了,应该将其pop出来,然后4 < s.top() = 5,所以5不应该pop。 +所以栈里的元素肯定是自底向上递减的,例如若nums=[1 6 5 3 4],则当遍历到3时,栈s为[6,5],而3 < s.top() = 5所以直到此时依然没有找到5的Next-Greater-Element,即5不应该pop出来。遍历到4时,s为[6,5,3],而4 > s.top() = 3, 所以3的Next-Greater-Element找到了,应该将其pop出来,然后4 < s.top() = 5,所以5不应该pop。 +时间复杂度O(n) # C++ ``` class Solution {