此题用普通模拟的方法遍历会很慢。高级的算法是利用到单调栈,时间复杂度是o(n)。
维护一个单调递减的栈,即当nums[i]小于栈顶元素时就不断入栈。当发现nums[i]大于栈顶元素时,说明nums[i]就是此栈顶元素所遇到的第一个greater number,把这个信息记录在一个Hash Map里,然后把栈顶元素退栈;重复上述操作直至nums[i]小于栈顶元素,再将其入栈并继续遍历nums。
GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||
GeezerVison is showing github.com — LeetCode/Stack/496.Next-Greater-Element-I at master · wisdompeak/LeetCode Expand file tree/ Copy path496.Next-Greater-Element-I/Directory actionsMore optionsMore optionsDirectory actionsMore optionsMore options/ Copy path496.Next-Greater-Element-I/Folders and files
此题用普通模拟的方法遍历会很慢。高级的算法是利用到单调栈,时间复杂度是o(n)。 维护一个单调递减的栈,即当nums[i]小于栈顶元素时就不断入栈。当发现nums[i]大于栈顶元素时,说明nums[i]就是此栈顶元素所遇到的第一个greater number,把这个信息记录在一个Hash Map里,然后把栈顶元素退栈;重复上述操作直至nums[i]小于栈顶元素,再将其入栈并继续遍历nums。 | |||||||||||||||||||||
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. | |||||||||||||||||||||