Write a method
isConsecutive
that takes a stack of integers as a parameter and that returns whether or not the stack contains a sequence of consecutive integers starting from the bottom of the stack (returning true if it does, returning false if it does not). Consecutive integers are integers that come one after the other, as in 5, 6, 7, 8, 9, etc. So if a stack s stores the following values:
bottom [3, 4, 5, 6, 7, 8, 9, 10] top
Then the call of isConsecutive(s) should return true. If the stack had instead contained this set of values:
bottom [3, 4, 5, 6, 7, 8, 9, 10, 12] top
Then the call should return false because the numbers 10 and 12 are not consecutive. Notice that we look at the numbers starting at the bottom of the stack.
The following sequence of values would be consecutive except for the fact that it appears in reverse order, so the method would return false:
bottom [3, 2, 1] top
Your method must restore the stack so that it stores the same sequence of values after the call as it did before. Any stack with fewer than two values should be considered to be a list of consecutive integers. You may use one queue as auxiliary storage to solve this problem.
I'm looking for ways to improve my future code. I was also wondering if it's better practice to use generics in cases like these? I'll also accept other methods of solving this problem as the correct answer. My main goal is to try to take away something that will make me a better programmer; if you have advice to shed, please don't hesitate to share it!
public boolean isConsecutive(Stack<Integer> s) {
boolean consecutive = true;
Queue<Integer> q = new LinkedList<Integer>();
while (!s.isEmpty()) {
q.offer(s.pop());
}
while (!q.isEmpty()) {
s.push(q.poll());
}
while (!s.isEmpty()) {
q.offer(s.pop());
}
while (q.size() > 1) {
int first = q.poll();
int second = q.poll();
consecutive &= (second - first == 1);
s.push(first);
s.push(second);
}
if (!q.isEmpty()) {
int first = q.poll();
int second = s.peek();
consecutive &= (second - first == 1);
s.push(first);
}
return consecutive;
}
Comparator
orBiPredicate
\$\endgroup\$