课程: Java Algorithms

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Stack algorithms: Next greater element

Stack algorithms: Next greater element - Java教程

课程: Java Algorithms

Stack algorithms: Next greater element

- [Instructor] Let's translate the next greater element algorithm into Java code. The first step is to create a function. Then we'll check our base case. If the array is empty, we'll print out an empty line and return. If the array has items, we'll initialize our stack and push the first element of the array onto the stack. In this algorithm, we're going to iterate through the array, trying to find the next greater element for each item. In each iteration, we're checking whether the current item in the array is the next greater element for the item at the top of the stack. If it is, we'll display it to the console but no matter what, we'll want to push this next element onto the stack. If it is, we'll display it to the console but no matter what, we'll want to push the next element onto the stack so we can find its next greater element. To check if the next variable is the next greater element for the item on top of the stack, we need to make sure the stack is not empty. If it does…

内容