//反转一个栈
public void reverse(Stack<Integer> stack) {
if (stack.isEmpty()) return;
int value = getBottom(stack);
reverse(stack);
stack.push(value);
}
private int getBottom(Stack<Integer> stack) {
int num = stack.pop();
if (stack.isEmpty()) {
return num;
} else {
int ans = getBottom(stack);
stack.push(num);
return ans;
}
}
2020-08-17-O(1)空间复杂度的反转栈
最新推荐文章于 2021-07-08 22:46:31 发布