![](https://img-blog.csdnimg.cn/20201014180756922.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
数据结构及算法
专栏收录数据结构及算法题
jazon@
芜湖
展开
-
缓存淘汰策略-LRU
LRU(Least Recently Used)LRU 算法的设计原则是:如果一个数据在最近一段时间没有被访问到,那么在将来它被访问的可能性也很小。也就是说,当限定的空间已存满数据时,应当把最久没有被访问到的数据淘汰。提供一种基于map+链表的LRU缓存实现public class LRUBaseHashMap { private HashMap<String, LRUNod...原创 2020-04-11 15:05:41 · 127 阅读 · 0 评论 -
多重背包
多重背包朴素算法这样就转换为我们之前分析过的01背包了。朴素算法的优化上面的算法都是将多重背包转换为01背包实现的解。原创 2020-04-11 14:45:27 · 167 阅读 · 0 评论 -
部分背包
部分背包问题:有不同种类的金属粉末,铜粉有5g价值10元,镁粉有10g价值20元,金粉8g价值21元,现在有一10g的背包,问装走最大价值粉末的策略。思路:贪心算法,直接先选单价最贵的粉末的装,然后再选次贵的,循环下去,知道装满背包。...原创 2020-04-11 14:40:18 · 170 阅读 · 0 评论 -
完全背包
完全背包一个背包总容量为V,现在有N个物品,第i个 物品体积为weight[i],价值为value[i],每个物品都有无限多件,现在往背包里面装东西,怎么装能使背包的内物品价值最大?思路从第一种物品开始,容量递增,来计算最大价值。第n种物品,则是将当前容量对应已有最大的价值和放入后的价值比较,取最大的。完全背包的状态转移方程if (oneWeight[i] <= j) { ...原创 2020-04-11 14:38:54 · 108 阅读 · 0 评论 -
01背包
01背包给定 n 种物品和一个容量为 C 的背包,物品 i 的重量是 wi,其价值为 vi ,应该如何选择装入背包的物品,使得装入背包中的物品的总价值最大?思路:解决办法:声明一个 大小为 m[n][c] 的二维数组,m[ i ][ j ] 表示 在面对第 i 件物品,且背包容量为 j 时所能获得的最大价值 ,那么我们可以很容易分析得出 m[i][j] 的计算方法,(1). j <...原创 2020-04-11 14:37:01 · 135 阅读 · 0 评论 -
区间DP浅析
区间dp区间dp就是在区间上进行动态规划,求解一段区间上的最优解。主要是通过合并小区间的 最优解进而得出整个大区间上最优解的dp算法。解决思路让我求解在一个区间上的最优解,那么我把这个区间分割成一个个小区间,求解每个小区间的最优解,再合并小区间得到大区间即可。所以在代码实现上,我可以枚举区间长度len为每次分割成的小区间长度(由短到长不断合并),内层枚举该长度下可以的起点,自然终点也就明了了...原创 2020-04-11 14:33:43 · 158 阅读 · 0 评论 -
利用环形队列+CAS实现无锁队列
利用环形队列+CAS实现无锁队列优点:1.保证元素的先进先出2.元素空间可以重复利用,因为是环形的。3.为多线程数据通信提供了一种高效的机制,比BlockingQueue速度要快。下面提供一种Java实现:public class LockFreeQueue { private AtomicReferenceArray atomicReferenceArray; priviat...原创 2019-02-03 10:40:12 · 907 阅读 · 0 评论 -
leetCode:Next Permutation
Next Permutation题目Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lo原创 2017-06-13 00:43:37 · 322 阅读 · 0 评论 -
leetCode:Divide Two Integers
Divide Two Integers题目Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.求两个数相除,但不允许使用乘法,除法和余数操作符思路最直观的方法是,用被除数逐个的减去除数,直到被除数小于0。这样做会超时。 那么如果每次不原创 2017-06-10 13:55:05 · 239 阅读 · 0 评论 -
leetCode:Swap Nodes in Pairs
Swap Nodes in Pairs题目Given a linked list, swap every two adjacent nodes and return its head.For example, Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only const原创 2017-06-08 19:20:42 · 286 阅读 · 0 评论 -
leetCode:Generate Parentheses
Generate Parentheses题目Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is:[ "((()))", "(()())", "(原创 2017-06-06 00:08:35 · 243 阅读 · 0 评论 -
leetCode:Remove Nth Node From End of List
题目Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked原创 2017-06-01 18:40:32 · 245 阅读 · 0 评论 -
leetCode:4Sum
4Sum题目Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note: The solution原创 2017-05-31 23:30:42 · 267 阅读 · 0 评论 -
leetCode:Letter Combinations of a Phone Number
题目Letter Combinations of a Phone NumberGiven a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) i原创 2017-05-31 16:10:51 · 456 阅读 · 0 评论 -
leetCode:3SumClosest
题目Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactl原创 2017-05-30 22:55:05 · 377 阅读 · 0 评论 -
leetCode:Integer to Roman
题目Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.题目分析将阿拉伯数字转换为罗马数字。思路建立表格,将要求的数从大到小分解。AC代码public class Solution { public String intToRoman原创 2017-05-28 12:40:43 · 340 阅读 · 0 评论 -
leetCode:Container With Most Water
题目Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two l原创 2017-05-28 01:08:45 · 269 阅读 · 0 评论