Skip to content

Commit e774b6c

Browse files
committed
实现LRU缓存
1 parent c89b3e8 commit e774b6c

4 files changed

Lines changed: 131 additions & 1 deletion

File tree

pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,18 @@
2121
<version>2.2.2</version>
2222
</dependency>
2323
</dependencies>
24+
<build>
25+
<plugins>
26+
<plugin>
27+
<groupId>org.apache.maven.plugins</groupId>
28+
<artifactId>maven-compiler-plugin</artifactId>
29+
<version>3.3</version>
30+
<configuration>
31+
<source>1.8</source>
32+
<target>1.8</target>
33+
<encoding>UTF-8</encoding>
34+
</configuration>
35+
</plugin>
36+
</plugins>
37+
</build>
2438
</project>

src/main/java/com/algorithm/study/demo/algorithm/FindProject.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
package com.algorithm.study.demo.algorithm;
22

3+
import com.alibaba.fastjson.JSON;
4+
import com.sun.deploy.util.StringUtils;
5+
6+
import java.util.*;
7+
import java.util.regex.Matcher;
8+
import java.util.regex.Pattern;
9+
310
/**
411
* 查找算法
512
* Created by liuxun on 2017/4/25.
613
*/
714
public class FindProject {
815
public static void main(String[] args) {
9-
// int[] is = new int[]{1, 2, 2, 4,6,6};
16+
1017
// System.out.println(search(is,is.length-1,6));
1118
// System.out.println(binarySearch(is,2));
1219
// Shape redRectangle = new RedShapeDecorator(new Rectangle());
1320
// redRectangle.doShaper();
21+
22+
1423
}
1524

25+
26+
1627
/**
1728
* @param nums: The integer array.
1829
* @param target: Target to find.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.algorithm.study.demo.algorithm;
2+
3+
import com.sun.deploy.util.StringUtils;
4+
5+
import java.util.*;
6+
import java.util.regex.Matcher;
7+
import java.util.regex.Pattern;
8+
9+
/**
10+
* 祖玛消除算法
11+
* @Author: liuxun
12+
* @CreateDate: 2018/11/6 下午3:58
13+
* @Version: 1.0
14+
*/
15+
public class ZumaProject {
16+
public static void main(String[] args) {
17+
String[] is = new String[50];
18+
Random ra =new Random();
19+
for(int i=0;i<50;i++){
20+
is[i]=ra.nextInt(3)+"";
21+
}
22+
//把输入的数据转换成字符串。这样可以利用正则表达式去查找重复数字
23+
String input=StringUtils.join(Arrays.asList(is),"");
24+
System.out.println("初始化数据:"+input);
25+
String newInput=matcher(input);
26+
System.out.println("去掉初始化数据连续出现三次级以上数字后的结果:"+newInput);
27+
28+
Scanner sc = new Scanner(System.in);
29+
System.out.println("输入需要消除的数字下标:");
30+
int index=sc.nextInt();
31+
String findInput=find(index,newInput);
32+
System.out.println("消除后的结果:"+findInput);
33+
34+
}
35+
public static String matcher(String input)
36+
{
37+
//创建一个List 用于存放重复3次或者以上的的数字
38+
List<String> list = new ArrayList<String>();
39+
//创建匹配的模式
40+
Pattern pattern = Pattern.compile("(\\w)\\1{2,}");
41+
//匹配器
42+
Matcher matcher = pattern.matcher(input);
43+
//查找与该模式匹配的子序列。从input 里面 查找出 与 此模式 "(\w)\1{2,}" 相匹配的 子序列。如果存在,返回true,如果不存在,返回false.
44+
while (matcher.find())
45+
{
46+
//返回匹配的子序列,并加入到list里面。
47+
list.add(matcher.group());
48+
}
49+
if (list.isEmpty()){
50+
System.out.println("当前数据没有出现连续三次或以上的数字");
51+
return input;
52+
}
53+
System.out.println("当前连续三次或以上出现的数字为:"+list);
54+
//找到连续重复的字符,加入到数组中。
55+
String[] strings = list.toArray(new String[0]);
56+
//找出连续并且重复的子序列。并且把这些连续重复的子序列用空字符串替换。
57+
for(int i=0 ;i<=strings.length-1;i++){
58+
if(strings[i].length()>1){
59+
// System.out.println(strings[i]);
60+
input=input.replace(strings[i],"");
61+
// System.out.println(input);
62+
}
63+
}
64+
//返回把连续重复的字符赐除掉的字符序列。
65+
return input;
66+
}
67+
68+
/**
69+
* 删除字符串指定index的元素
70+
* @param index
71+
* @param Str
72+
* @return
73+
*/
74+
public static String removeChar(int index,String Str) {
75+
Str = Str.substring(0, index) + Str.substring(index + 1, Str.length());//substring的取值范围是:[,)
76+
return Str;
77+
}
78+
/**
79+
* 用户需要消除的数字下标以及数字
80+
* 思路就是删掉用户查找的那个数字然后返回新的字符串
81+
* 比如{11212},用户传入下标为2的数字2返回"1112"
82+
* 然后把字符串继续重复调用matcher即可
83+
* @param index
84+
* @return
85+
*/
86+
public static String find(int index,String array){
87+
String newArr=removeChar(index,array);
88+
String matcherArr = matcher(newArr);
89+
if (newArr.equals(matcherArr)){
90+
//如果用户输入数据后没有发现连续三次级以上的数据就返回原始数据
91+
return array;
92+
}
93+
return matcherArr;
94+
}
95+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.algorithm.study.demo.datastructure.tree;
2+
3+
/**
4+
* AVL二叉搜索
5+
* @Author: liuxun
6+
* @CreateDate: 2018/10/22 上午11:26
7+
* @Version: 1.0
8+
*/
9+
public class AVLBinTree {
10+
}

0 commit comments

Comments
 (0)