剑指offer刷题
面试过程中剑指offer涉及的几十道算法题
不撞南天门不回头
work hard, play hard
展开
-
弗兰西刷题日记-剑指offer-41-数据流的中位数
利用一个大顶堆(存储左半边元素)和一个小顶堆(存储右半边元素) import java.util.*; class MedianFinder { /** initialize your data structure here. */ PriorityQueue<Integer> maxHeap; PriorityQueue<Integer> minHeap; public MedianFinder() { maxHeap =原创 2020-10-17 21:23:21 · 86 阅读 · 0 评论 -
弗兰西刷题日记-剑指offer-40-最小的k个数
解法一:构造最大堆(堆顶元素是堆中元素最大值) import java.util.*; class Solution { public int[] getLeastNumbers(int[] arr, int k) { if(arr == null || arr.length < k || k == 0) return new int[0]; PriorityQueue<Integer> maxHeap = new Prior原创 2020-10-17 19:00:51 · 78 阅读 · 0 评论 -
弗兰西刷题日记-剑指offer-59(2)-队列的最大值
import java.util.*; class MaxQueue { private Queue<Integer> queue; private LinkedList<Integer> max; public MaxQueue() { queue = new LinkedList<>(); max = new LinkedList<>(); } public int max原创 2020-10-16 22:03:04 · 112 阅读 · 0 评论 -
弗兰西刷题日记-剑指offer-30-包含min函数的栈
import java.util.*; class MinStack { private Stack<Integer> stack; private Stack<Integer> minStack; /** initialize your data structure here. */ public MinStack() { stack = new Stack<>(); minStack = new Sta原创 2020-10-16 17:13:39 · 91 阅读 · 0 评论 -
弗兰西刷题日记-剑指offer-09-两个栈实现队列
import java.util.*; class CQueue { private Stack<Integer> stack1; private Stack<Integer> stack2; public CQueue() { stack1 = new Stack<>(); stack2 = new Stack<>(); } public void appendTail(in原创 2020-10-16 17:04:20 · 115 阅读 · 0 评论 -
斐波那契避免爆栈
题目来源 题目信息 思路介绍 可以从递归和迭代两个来考虑,传统递归的话容易发生爆栈,所以本题考虑使用迭代的方式。 代码展示 class Solution { public int fib(int n) { if(n < 0){ return -1; } else if(n == 0){ ...原创 2020-05-03 19:20:24 · 251 阅读 · 0 评论 -
栈模拟队列尾插和头删
题目来源 题目信息 思路介绍 这道题简单难度,关键是联想到栈和队列的区别,栈——先进后出,队列——先进先出。 所以要想使用栈来模拟队列,我们可以想到使用两个栈不断地“倒腾”数据: 在添加元素到尾部的时候,若是栈一有元素,将全部元素压入栈二,然后再直接将元素压入栈二 在删除队列头部元素时,将栈二的元素全部出栈到栈一,此时栈一顶部元素就是队列头部元素 代码展示 class CQueue{ S...原创 2020-05-03 19:00:00 · 369 阅读 · 0 评论