avatar

`算法`标签下的文章

未分类

淘汰算法LRU

基于双链表实现LRU
原理: 将Cache的所有位置都用双连表连接起来,当一个位置被命中之后,就将通过调整链表的指向,将该位置调整到链表头的位置,新加入的Cache直接加到链表头中。这样,在多次进行Cache操作后,最近被命中的,就会被向链表头方向移动,而没有命中的,而想链表后面移动,链表尾则表示最近最少使用的Cache。当需要替换内容时候,链表的最后位置就是最少被命中的位置,我们只需要淘汰链表最后的部分即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
public class LRUCache {  

//缓存大小
private int cacheSize;
//缓存容器
private Hashtable<K, CacheNode<K, V>> nodes;
//当前缓存对象数量
private int currentSize;
//(实现双链表)链表头
private CacheNode first;
//(实现双链表)链表尾
private CacheNode last;

public LRUCache(int i) {
currentSize = 0;
cacheSize = i;
nodes = new Hashtable(i);//缓存容器
}

/**
* 链表节点
*
*/
private static class CacheNode<K,V> {
CacheNode prev;
CacheNode next;
K key;
V value;
CacheNode() {
}
}

public V get(K key){
CacheNode<K, V> node = nodes.get(key);
if(node!=null){
moveToHead(node);
return node.value;
}else{

return null;
}
}

public void put(K key, V value){
CacheNode<K, V> node = nodes.get(key);
if(node == null){
if(currentSize >= cacheSize){
if(last != null){
nodes.remove(last.key);
}
removeLast();
}else{
currentSize++;
}
node = new CacheNode<>();
}
node.value=value;
node.key=key;
moveToHead(node);
nodes.put(key, node);
}

private void removeLast(){
if(last!=null){
if(last.prev != null){
last.prev.next = null;
}else{
first = null;
}
last = last.prev;
}
}

private void moveToHead(CacheNode node){
if(node == first){
return;
}

if (node.prev != null) {
node.prev.next = node.next;
}
if (node.next != null) {
node.next.prev = node.prev;
}
if (last == node) {
last = node.prev;
}
if (first != null) {
node.next = first;
first.prev = node;
}
first = node;
node.prev = null;
if (last == null) {
last = first;
}
}
}

阅读剩下更多

默认配图
未分类

链表相关算法

1.删除链表指定节点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class LinkedNode{
public LinkedNode removeElement(LinkedNode head, int val){

//先从头结点开始删
while(head != null && head.val == val){
head = head.next;
}

if(head==null){
return head;
}

LinkedNode pre = head;
while(pre.next!=null){
if(pre.next.val==val){
pre.next = pre.next.next;
}else{
pre = pre.next;
}
}
return head;
}
}

2.有序链表插入元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class LinkedNode{
public void insertElement(LinkedNode node){
LinkedNode pre = null;
LinkedNode current = first;
while(current!=null&&node.value<current.value){
pre = current;
current = current.next;
}

if(pre==null){
first = node;
}else{
pre.next = node;
}
node.next = current;
}
}

3.判断链表是否有环

1
2
3
4
5
6
7
8
9
10
11
12
13
public static <T> boolean isLoopList(ListNode<T> head){
ListNode<T> slowPointer, fastPointer;

slowPointer = fastPointer = head;
while(fastPointer != null && fastPointer.next != null){
slowPointer=slowPointer.next;
fastPointer=fastPointer.next.next;
if(slowPointer == fastPointer){
return true;
}
}
return false;
}

阅读剩下更多

默认配图
未分类

二叉树的一些操作

反转二叉树

前一段时间国外某大牛面试Google被刷掉,原因是没有能够徒手写出来反转二叉树的代码,在网上看到了一个人的分析代码,特此记下来。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return null;
}
root.left = invertTree(root.left);
root.right = invertTree(root.right);

TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;
return root;
}
}

阅读剩下更多

默认配图
未分类

经典排序算法

本次介绍几种经典排序算法以及Java实现代码
1.冒泡排序
冒泡排序是属于相邻两两比较进行排序的一种,由于其实现是经过1到n-1轮排序,
通过比较相邻的两个数,大的向下继续与相邻的进行比较,直到找到正确的顺序位置。

1
2
3
4
5
6
7
8
9
10
11
public void bubbleSort(int[] a) {
for (int i = 0; i < a.length - 1; i++) {//比较轮次,经过n-1次比较
for (int j = 0; j < a.length - 1 - i; j++) {//每次比较相邻的两个数,大的向后移动
if (a[j] > a[j + 1]) {
int temp = a[j + 1];
a[j + 1] = a[j];
a[j] = temp;
}
}
}
}

阅读剩下更多

默认配图
未分类

二分查找Java实现

这个栗子是用Java实现的二分查找

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class BinarySearch {

public static int binarySearch(int array[], int number, int value) {
int left = 0;
int right = number - 1;
while (left <= right) {
int middle = left + ((right - left) >> 1);
if (array[middle] > value) {
right = middle - 1;
} else if (array[middle] < value) {
left = middle + 1;
} else {
return middle;
}
}
return -1;
}
}

阅读剩下更多

默认配图
未分类

求最大子序列和的问题

问题:给定(可能有负数)整数A1,A2,…,An,求∑Ak的最大值。(所有整数均为负数,则最大子序列和为0)。

1.穷举法
这种算法就是穷举所有的可能。for循环中的循环变量反映了Java中数组从0开始而不是从1开始这样一个事实。还有,本算法并不计算实际的子序列;实际的计算还要添加一些额外的代码。这个算法的运行时间为O(N3)

1
2
3
4
5
6
7
8
9
10
11
12
13
public static int maxSubSum1(int[] a) {
int maxSum = 0;
for(int i = 0; i < a.length; i++)
for(int j = i; j < a.length; j++){
int thisSum = 0;
for(int k = i; k <= j; k++)
thisSum += a[k];

if(thisSum > maxSum)
maxSum = thisSum;
}
return maxSum;
}

阅读剩下更多

默认配图
返回顶部