时间复杂度: 跟数据有关系
常数操作: 完成操作的时间跟数据量无关, 固定的值
最优解: 先时间复杂度, 再空间复杂度
常见数据结构
队列: 先进先出(2个指针)
栈: 先进后出(1个指针), pop(出栈), push(压栈), peek(取值) 2者之间可以通过指针来判断
如何使用
队列结构实现栈,栈结构实现队列
堆: 逻辑上是完全二叉树的结构, 大根堆(大值在上), 小根堆(小值在上), 实际结构是数组实现, Java中PriorityQueue(优先级队列)进行实现
递归处理
所有的递归操作都可以拆解为非递归(自己压栈), 递归操作就是栈的操作
/**
* 递归的本质就是保存当前的变量再去进行调用函数
*
* @param stack
* @return
*/
private static <T> T getLastElement(Stack<T> stack) {
// 函数递归就是先变量压栈然后在出栈
T element = stack.pop();
if (stack.isEmpty()) {
return element;
}
// 同步调用
T last = getLastElement(stack);
stack.push(element);
return last;
}
public static <T> void reverse(Stack<T> stack) {
if (stack == null || stack.isEmpty()) {
return;
}
T last = getLastElement(stack);
reverse(stack);
stack.push(last);
}