![](https://img-blog.csdnimg.cn/20201014180756724.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
面试复习
凡凡轶崔
这个作者很懒,什么都没留下…
展开
-
【算法题】136 137 只出现一次的数,及其变种
力扣136 137原创 2022-08-17 11:11:33 · 161 阅读 · 1 评论 -
【算法】二叉树后序遍历(递归+非递归)
【算法】二叉树后序遍历(递归+非递归)原创 2022-08-15 14:46:11 · 194 阅读 · 0 评论 -
【算法题】单链表快速排序
Java实现单链表的快速排序和归并排序 - morethink - 博客园在一般实现的快速排序中,我们通过首尾指针来对元素进行切分,下面采用快排的另一种方法来对元素进行切分。我们只需要两个指针p1和p2,这两个指针均往next方向移动,移动的过程中保持p1之前的key都小于选定的key,p1和p2之间的key都大于选定的key,那么当p2走到末尾时交换p1与key值便完成了一次切分。图示如下:public class Test23 { public static void mai原创 2022-03-08 22:36:33 · 426 阅读 · 0 评论 -
【算法题】打印二叉树中和为某一值的所有路径
public class Test17 { //打印二叉树中和为某一个值的所有路径 public static ArrayList<ArrayList<Integer>> pathList = new ArrayList<>(); public static ArrayList<Integer> path = new ArrayList<>(); public static ArrayList<Array.原创 2022-02-17 16:57:27 · 296 阅读 · 0 评论 -
【算法题】二叉树节点有指向parent父节点的指针,返回输入节点在这个树中序遍历序列里下一个节点
public class Test21 { //二叉树,left right parent父节点指针 //返回输入节点在这个树中序遍历序列里下一个节点 static class TreeNode { int val; TreeNode parent, left, right; public TreeNode(int val, TreeNode parent, TreeNode left, TreeNode right) { .原创 2022-02-17 16:46:14 · 606 阅读 · 0 评论 -
【算法题】二叉搜索树中的众数
给定 BST [1,null,2,2], 1 \ 2 / 21 2 3 4 5 返回 [2]提示:如果众数超过1个,不需考虑输出顺序public class Test20 { static class TreeNode { int val; TreeNode left, right; public TreeNode(int val, TreeNode left, TreeNod原创 2022-02-16 15:23:39 · 176 阅读 · 0 评论 -
guava cache
guava超时及刷新机制首先看一下三种基于时间的清理或刷新缓存数据的方式:expireAfterAccess:当缓存项在指定的时间段内没有被读或写就会被回收。expireAfterWrite:当缓存项在指定的时间段内没有更新就会被回收。refreshAfterWrite:当缓存项上一次更新操作之后的多久会被刷新。考虑到时效性,我们可以使用expireAfterWrite,使每次更新之后的指定时间让缓存失效,然后重新加载缓存。guava cache会严格限制只有1个加载操作(load.原创 2022-02-16 10:39:51 · 459 阅读 · 0 评论 -
【算法题】二叉树中的最大路径和
路径 被定义为一条从树中任意节点出发,沿父节点-子节点连接,达到任意节点的序列。同一个节点在一条路径序列中 至多出现一次 。该路径 至少包含一个 节点,且不一定经过根节点。路径和 是路径中各节点值的总和。给你一个二叉树的根节点 root ,返回其 最大路径和 。输入:root = [-10,9,20,null,null,15,7]输出:42解释:最优路径是 15 -> 20 -> 7 ,路径和为 15 + 20 + 7 = 42以A为根节点的这棵树的最大路径和可.原创 2022-02-15 13:36:15 · 259 阅读 · 0 评论 -
【算法题】二叉树寻找最小公共祖先
输入:4,5。输出:2。输入:4,9。输出:1.从root开始遍历,如果n1和n2中的任一个和root匹配,那么root就是LCA。 如果都不匹配,则分别递归左、右子树,如果有一个 key(n1或n2)出现在左子树,并且另一个key(n1或n2)出现在右子树,则root就是LCA. 如果两个key都出现在左子树,则说明LCA在左子树中,否则在右子树。public class Test16 { static class Node { int val; ...原创 2022-02-11 23:16:52 · 657 阅读 · 0 评论 -
【算法题】将链表根据元素的位置模K重组
给定链表与数字K,根据每个节点的位置,对K取余,根据模后的值从小到大重组链表,同余数值的节点保持元素节点相对位置不变。要求只遍历一次链表输入:0-1->2->3->4k=2输出:0->2->4->1->3输入:0-1->2->3->4->5->6->7k=3输出:0->3->6->1->4->7->2->5public class原创 2022-02-11 11:48:07 · 339 阅读 · 0 评论 -
【算法题】求最大连续1的个数
给定一个由若干 0 和 1 组成的数组 A,我们最多可以将 K 个值从 0 变成 1 。返回仅包含 1 的最长(连续)子数组的长度。输入:A = [1,1,1,0,0,0,1,1,1,1,0], K = 2输出:6输入:A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3输出:10方法一:滑动窗口法,时间复杂度O(n)思路:其实,将K个0转换为1之后,连续的1组成的子数组长度为最长,这个问题可以转换为:在数组中取一个子数组原创 2022-02-10 16:32:59 · 1007 阅读 · 0 评论 -
【算法题】寻找链表环的入口
慢走了a+x,快的是慢的2倍,得出2(a+x) = a + nc + x, n为快指针走了环的圈数化简,2a + 2x = a + nc + xa + x = nc a = nc - x a = (n-1)c + (c - x) a = kc + (c-x) , k为常量n-1即,一个指针从头部走a步,另一个从相遇节点走k次环再走(c-x)剩余步数,即是入口节点。...原创 2022-01-27 17:27:33 · 410 阅读 · 0 评论 -
【算法题】超长大数相减
import org.apache.commons.lang.StringUtils;public class Test8 { public static final int NINE = 9; public static final char ZERO = '0'; public static void main(String[] args) { String b = "8951331230000000000000000000000001"; .原创 2022-01-26 20:38:17 · 384 阅读 · 0 评论 -
【算法题】求数组除当前位其他数的乘积
public class Test7 { // 输入: 一个长度为n的整数数组input// 输出: 一个长度为n的数组result,满足result[i] = input数组中,除了input[i] 之外的所有数的乘积,不用考虑溢出// 不能使用除法// 例如 input {2, 3, 4, 5} output: {60, 40, 30, 24} public static void main(String[] args) { int[] i.原创 2022-01-21 16:54:14 · 255 阅读 · 0 评论 -
【算法题】判断树是否是镜像的
public class TestD {// 1// 2 2// 3 4 4 3 public static void main(String[] args) { } public static boolean judgeTreeIsMirror(TreeNode head) { if (head == null) { return tru.原创 2022-01-19 12:10:09 · 200 阅读 · 0 评论 -
【算法题】求第N个丑数
public class Test6 { public static void main(String[] args) { System.out.println(getUglyNumber(10)); } public static int getUglyNumber(int n) { if (n <= 0) { return 0; } int[] arr = new int[n]; .原创 2022-01-17 20:59:54 · 143 阅读 · 0 评论 -
【算法题】把数组中的数排成一个最小的数
import com.dianping.cat.util.StringUtils;import java.util.Arrays;import java.util.Comparator;public class Test5 { public static void main(String[] args) { int[] input = {3, 32, 321}; System.out.println(joinToMinNumber(input)); .原创 2022-01-17 20:26:54 · 425 阅读 · 0 评论 -
【算法题】从1到n整数中1出现的次数
public class Test4 {// 我的想法是,每一次求一个位置上的1的个数,第一轮先求个位上的1出现的个数,第二轮求十位上1出现的个数,以此类推...这样就能保证每个位置上的1都数了一次,并且不会出现多次重复数的情况。现在以其中的某一轮循环为例:假设现在有一个数 n = abcd(a,b,c,d分别表示0 ~ 9中的任意数)。我们本次循环要数的是b所在位置上1出现的个数。也就是要求满足X1XX这个样子的数的个数。我们可以将abcd分成两个部分,一部分在b前面即a,另一部分在b后面即cd.原创 2022-01-17 17:13:04 · 64 阅读 · 0 评论 -
【算法题】快速排序--递归
public class QuickSort { public static int partition(int[] data, int low, int high) { int key = data[low]; while (low < high) { while (data[high] >= key && low < high)原创 2017-10-26 18:56:26 · 241 阅读 · 0 评论 -
【算法题】找出数组最小k个数
思路:总体类似二分查找,部分类似快排。将小于当前元素的数,都放到元素左侧。返回位置,当位置大于K-1时,在左侧找;反之在右侧找。找到第K-1个位置的数后,输出前k个即可。public class Test3 { public static void main(String[] args) { int[] arr = {3, 4, 6, 5, 3, 2, 1}; int k = 3; findKData(arr, k); } pu原创 2022-01-06 16:04:01 · 81 阅读 · 0 评论 -
【算法题】数组中出现次数超过一半的数字
public class Test { public static void main(String[] args) { int[] chars = {1, 2, 1, 4, 1, 6, 1, 8, 1, 10, 1}; System.out.println(moreThanHalfNum(chars)); } public static int moreThanHalfNum(int[] nums) { int count = .原创 2022-01-02 17:04:45 · 68 阅读 · 0 评论 -
【算法题】打印字符串中所有字符的排列
题目描述:输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输 入字符串 abc,则打印出由字符 a,b,c 所能排列出来的所有字符串 abc,acb,bac,bca,cab 和 cba。 思路:将当前位置的字符和前一个字符位置交换,递归。public class Test { public static void main(String[] args) { char[] chars = {'a', 'b', 'c'}; List&原创 2022-01-02 16:29:30 · 206 阅读 · 0 评论 -
【算法题】二维数组,顺时针遍历(m*n矩阵)
public static void main(String[] args) { //1 2 3 4 //10 11 12 5 //9 8 7 6 //输出:1 2 3 6 9 8 7 4 5// int[][] arr = {{1, 2, 3, 4}, {10, 11, 12, 5}, {9, 8, 7, 6}}; //1 2 3 //8 9 4 //7 6 5/.原创 2021-12-29 17:57:27 · 503 阅读 · 0 评论 -
【算法题】二分法变形,在一个两段有序的数组中查找,该数组分两段有序,第一段的数都大于第二段的数。
public static int partition(int[] arr, int left, int right, int key) { if (left > right) { return -1; } int mid = (left + right) / 2; if (arr[mid] == key) { return mid; } if (...原创 2021-12-27 20:05:40 · 268 阅读 · 0 评论 -
【算法题】求数组中第K大元素
思路:快排变种public class ArrKBigger { public static void main(String[] args) { int arr[] = {9,8,7,6,5,4,3,2,1,0}; int k = 6; System.out.println(quickSort(arr, 0, arr.length-1, k)); } public static int quickSort(int[] arr,原创 2021-12-13 19:21:47 · 204 阅读 · 0 评论 -
redis数据结构底层实现
原创 2021-12-08 19:45:28 · 436 阅读 · 0 评论 -
【算法题】两个有序数组的交集
public class JiaoJi { static List<Integer> list1 = Lists.newArrayList(1, 2, 3, 3, 4, 6, 7, 7, 8, 9, 12); static List<Integer> list2 = Lists.newArrayList(3, 3, 4, 5, 6, 6, 10, 10, 13); public static void main(String[] args) { .原创 2021-12-01 19:26:50 · 161 阅读 · 0 评论 -
死锁代码示例
public class DeadLock { public static void main(String[] args) { Object object1 = new Object(); Object object2 = new Object(); MyThread myThread1 = new MyThread(object1,object2,true); MyThread myThread2 = new MyThread(.原创 2021-12-01 18:01:33 · 436 阅读 · 0 评论 -
InnoDB行记录格式及BLOB/TEXT字段优化
原创 2021-08-31 11:42:14 · 100 阅读 · 0 评论 -
【算法题】三个线程交替打印abc
public class ABC_Synch { public static class ThreadPrinter implements Runnable { private String name; private Object prev; private Object self; private ThreadPrinter(String name, Object prev, Object self) { .原创 2021-07-15 15:10:18 · 70 阅读 · 0 评论 -
【算法题】归并排序——递归
public class MergeSort { public static void main(String[] args) { int[] data = {8, 7, 7, 9, 2, 5, 1, 4, 3}; mergeSort(data, 0, data.length-1); for(int i=0;i<data.length;i++){ System.out.print(data[i] + " "); .原创 2021-07-14 11:39:10 · 65 阅读 · 0 评论 -
【算法题】删除链表倒数第n个节点
public ListNode removeNthFromEnd (ListNode head, int n) { // write code here Stack<ListNode> stack = new Stack(); ListNode tmp, nhead; nhead = new ListNode(-1); nhead.next = head; tmp = nh...原创 2021-07-06 19:50:33 · 100 阅读 · 1 评论 -
【算法题】二叉树层级遍历 list套list
import org.apache.commons.collections.CollectionUtils;import org.eclipse.jetty.util.BlockingArrayQueue;import scala.concurrent.forkjoin.LinkedTransferQueue;import java.util.ArrayList;import java.util.Queue;import java.util.concurrent.ArrayBlock...原创 2021-07-06 19:47:13 · 78 阅读 · 0 评论 -
【算法题】链表翻转
public class JavaTest1 { public static void main(String[] args) { Node head = new Node(0); Node node1 = new Node(1); Node node2 = new Node(2); Node node3 = new Node(3); head.setNext(node1); node1.setNext(node2); node2.setNext(node3); .原创 2021-07-02 17:47:39 · 52 阅读 · 0 评论 -
【算法题】字符串翻转。例如,输入“I am a student.”,则输出“student. a am I”。
public class ReverseString { public static void main(String[] args) { String str="I am a student."; char[] chars=str.toCharArray(); //first reverse reverse(chars,0,chars.length-1); System.out.println(String.va.原创 2021-07-02 17:34:24 · 557 阅读 · 0 评论 -
【算法题】求两个链表第一个交点
class Node { int num; Node next; public Node(int num) { this.num = num; }}public class LinkFirstNode { public static void main(String[] args) { Node node1 = new Node(1); Node node2 = new Node(2); Nod.原创 2021-07-02 16:11:40 · 79 阅读 · 0 评论 -
【算法题】交替打印0和1
public class JiaoTiPrint { public static void main(String[] args) throws InterruptedException { Object lock = new Object(); OneThread oneThread = new OneThread(lock, 1000); ZeroThread zeroThread = new ZeroThread(lock, 1000); .原创 2021-07-02 15:55:16 · 165 阅读 · 0 评论 -
【算法题】给定一个数组,连续一段相加,求连续相加最大和
public class LianXuMaxSum { public static void main(String[] args) { int[] nums = {-3, -2, -8, -1, -1, -10, -9, -1}; System.out.println(max(nums)); } public static int max(int[] nums) { int max = nums[0]; int .原创 2021-07-02 15:53:39 · 472 阅读 · 0 评论 -
【算法题】给定一个数组,相邻两数不能相加,求数组相加最大和
public class JianGeMaxSum { public static void main(String[] args) { int[] nums = {3, 2, 8, 1, -1, -10, 9, -1}; System.out.println(max(nums)); } public static int max(int[] nums) { if (nums.length == 0) { .原创 2021-07-02 15:52:25 · 1258 阅读 · 0 评论 -
【算法题】Java二叉树前序遍历(递归+非递归)
递归: public void preOrderRecur(Node root) { if (root == null) { return; } System.out.print(root.data + " -> "); preOrderRecur(root.left); preOrderRecur(root.right); }非递归:原创 2021-06-19 12:22:08 · 71 阅读 · 0 评论