课程: Java Algorithms

今天就学习课程吧!

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

Stack algorithms: Theorizing an algorithm

Stack algorithms: Theorizing an algorithm - Java教程

课程: Java Algorithms

Stack algorithms: Theorizing an algorithm

- [Instructor] As a software engineer, you'll need to know how to leverage the built-in stack functionality when working with algorithms that keep track of state. In this lesson, we'll look at how we can use the stack data structure to solve software problems. Let's say we want to print out the next greater element for every element in the array. The next greater element for a given element is the first greater element on the right side of the array. If no greater element exists, then the next greater element is a sentinel value, -1. For example, if we were given an array with 4, 5, 2 and 35, the next greater element for 4 would be 5. The next greater element for 5 would be 35 and the same for two. 35 does not have a next greater element to the right, so it's value would be -1. In another example, let's say we have 16, 7, 2 and 15. 16 does not have a next greater element, so it would be -1. 7's next greater element to the right is 15 and the same for 2. 16 is not to the right of 7 and…

内容