![](https://img-blog.csdnimg.cn/20200424114616374.jpg?x-oss-process=image/resize,m_fixed,h_224,w_224)
常见算法
记录做题的常用算法
香榭的落叶l
这个作者很懒,什么都没留下…
展开
-
三个线程交替打印
面试脑袋一片空白,只记得用condition。。靠了。import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;/** * @author 香榭的落叶 * @date 2020/3/22 * @describe */public class Game { public stat原创 2021-03-27 21:33:08 · 261 阅读 · 0 评论 -
并查集简单实现
class UnionFind { int[] data = new int[26]; public UnionFind() { for (int i = 0; i < data.length; i++) { data[i] = i; } } private int findRoot(int cur) { //路劲压缩,避免查找树过长 return cur == data[cur] ? cur : (data[cur] = findRoo原创 2020-10-13 12:53:03 · 102 阅读 · 0 评论 -
KMP算法
class Solution { public int strStr(String s1, String s2) { if (s1.length() < s2.length()) return -1; if(s2.length() == 0)return 0; int i = 0, j = 0; int[] next = getNextArr(s2); while (i < s1.length() && j < s2.length原创 2020-09-28 17:42:27 · 100 阅读 · 0 评论 -
Manacher算法简单实现
/** * @author: 1298509345 * date: 2020/8/23 * Time: 20:34 * Describe: */public class Main2 { static char[] manacherStr(String str) { char[] chars = str.toCharArray(); char[] ret = new char[chars.length * 2 + 1]; int idx = 0; for (i原创 2020-09-28 17:40:42 · 91 阅读 · 0 评论 -
字母交换
题目字符串S由小写字母构成,长度为n。定义一种操作,每次都可以挑选字符串中任意的两个相邻字母进行交换。询问在至多交换m次之后,字符串中最多有多少个连续的位置上的字母相同?链接思路记录每个字母出现的下标。对每一个字母出现的所有位置,进行滑窗计算移动代价。窗口从一个元素开始扩大,计算代价超过要求,缩小窗口,否则扩大窗口。由经验可得,要把滑窗内所有位置移动到连续位置,两边元素靠中间元素移动的代价较小。比如字母a的所有下标情况为[11,13,15,17,19|21,23,25,30,31],选取中原创 2020-08-19 13:59:57 · 1607 阅读 · 0 评论 -
栈实现二叉树迭代遍历
二叉树前序,中序,后序通用public List<Integer> postorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<>(); Deque<TreeNode> stack = new LinkedList<>(); if(root != null){ stack.push(root);原创 2020-07-21 18:07:53 · 220 阅读 · 0 评论 -
LeetCode146. LRU缓存机制
题目运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制。它应该支持以下操作: 获取数据 get 和 写入数据 put 。获取数据 get(key) - 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回 -1。写入数据 put(key, value) - 如果密钥已经存在,则变更其数据值;如果密钥不存在,则插入该组「密钥/数据值」。当缓存容量达到上限时,它应该在写入新数据之前删除最久未使用的数据值,从而为新的数据值留出空间。链接思路一开始想的是双端队原创 2020-05-25 11:51:18 · 117 阅读 · 0 评论 -
字典树字符串匹配
题目哦,不!你不小心把一个长篇文章中的空格、标点都删掉了,并且大写也弄成了小写。像句子"I reset the computer. It still didn’t boot!“已经变成了"iresetthecomputeritstilldidntboot”。在处理标点符号和大小写之前,你得先把它断成词语。当然了,你有一本厚厚的词典dictionary,不过,有些词没在词典里。假设文章用sentence表示,设计一个算法,把文章断开,要求未识别的字符最少,返回未识别的字符数。链接class Solutio原创 2020-07-09 11:12:29 · 437 阅读 · 0 评论 -
全排列回溯
输入一个字符串,打印出该字符串中字符的所有排列。class Solution { char[] c; boolean[] visit; public String[] permutation(String str) { List<String> res = new ArrayList<>(); c = str.toCharArray(); if(c.length == 0){原创 2020-07-06 21:46:41 · 141 阅读 · 0 评论 -
堆排序
public class HeapSort { public static void main(String[] args) { int[] arr = {7, 6, 7, 11, 5, 12, 3, 0, 1}; System.out.println("之前:" + Arrays.toString(arr)); heapSort(arr); System.out.println("升序:" + Arrays.toString(arr原创 2020-06-29 13:04:23 · 91 阅读 · 0 评论 -
归并排序
public class MergeSort { void mergeArray(int[] arr, int left, int mid, int right, int[] temp) { //[left,mid],[mid+1,right]的下标指针 int i = left, j = mid + 1; //temp下标 int idx = left; //把这两段有序地放入temp while (原创 2020-06-26 14:03:19 · 88 阅读 · 0 评论 -
二分查找
** 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。**class Solution { public int search(int[] nums, int target) { int left=0,right=nums.length; while(left<right){ int mid=left+(right原创 2020-05-09 11:20:20 · 180 阅读 · 0 评论 -
最长公共子序列(LCS)
public class LCS { public static String A = "13456778"; public static String B = "357486782"; public static void main(String[] args) { int[][] dp = new int[A.length() + 1][B.lengt...原创 2020-04-24 12:14:53 · 93 阅读 · 0 评论 -
最长上升子序列(LIS)
public class LIS { public static int lengthOfLIS(int[] nums) { /** * @discribe O(n ²)复杂度:dp[i]=max(dp[j]+1 if(j<i and nums[i]>nums[j])) * @Params [nums] *...原创 2020-04-24 12:08:08 · 91 阅读 · 0 评论 -
最长回文子串(中心扩展)
每次循环选择一个中心,进行左右扩展,判断左右字符是否相等。 public int expendCenter(String s, int left, int right) { int L = left, R = right; while (L >= 0 && R < s.length() && s.charAt(L) ==...原创 2020-04-24 12:05:48 · 418 阅读 · 0 评论 -
快速排序
public class QuickSort { public static void main(String[] args) { int[] arr = new int[]{4, 1, 8, 3, 5, 2}; new QuickSort().quickSort(arr, 0, arr.length - 1); System.out.pri...原创 2020-04-24 11:45:03 · 111 阅读 · 0 评论