Skip to content

Commit b1bd36d

Browse files
committedDec 14, 2017
upload
0 parents  commit b1bd36d

16 files changed

+251
-0
lines changed
 

‎.classpath

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/

‎.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>offer-java</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

‎.settings/org.eclipse.jdt.core.prefs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.7
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.7

‎README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# 数据结构与算法 面试题(Java版)
2+
3+
- [单例模式的六种实现](/review02)
4+
- [二位数组中的查找](/review03)
5+
- [字符串替换空格](/review04)

‎src/review02/README.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## 为什么使用单例模式
2+
需要确保某个类只要一个对象,或创建一个类需要消耗的资源过多,如访问IO和数据库操作等,这时就需要考虑使用单例模式了。
3+
4+
## 使用单例模式需要注意的关键点
5+
将构造函数访问修饰符设置为private
6+
通过一个静态方法或者枚举返回单例类对象
7+
确保单例类的对象有且只有一个,特别是在多线程环境下
8+
确保单例类对象在反序列化时不会重新构建对象
9+
## 单例模式的几种写法
10+
11+
### 1. 饿汉式
12+
[Singleton1.java](/Singleton1.java)
13+
### 2. 懒汉式
14+
getInstance()方法中添加了synchronized关键字,使其变成一个同步方法,目的是为了在多线程环境下保证单例对象唯一。
15+
16+
优点: 只有在使用时才会实例化单例,一定程度上节约了资源。
17+
缺点: 第一次加载时要立即实例化,反应稍慢。每次调用getInstance()方法都会进行同步,这样会消耗不必要的资源。这种模式一般不建议使用。
18+
19+
[Singleton2.java](/Singleton2.java)
20+
### 3. DCL(Double CheckLock)实现单例
21+
优点: 资源利用率高,既能够在需要的时候才初始化实例,又能保证线程安全,同时调用getInstance()方法不进行同步锁,效率高。
22+
缺点: 第一次加载时稍慢,由于Java内存模型的原因偶尔会失败。在高并发环境下也有一定的缺陷,虽然发生概率很小。
23+
DCL模式是使用最多的单例模式实现方式,除非代码在并发场景比较复杂或者JDK 6以下版本使用,否则,这种方式基本都能满足需求。
24+
25+
[Singleton3.java](/Singleton3.java)
26+
### 4. 静态内部类
27+
第一次加载Singleton类时不会初始化instance,只有在第一次调用getInstance()方法时,虚拟机会加载SingletonHolder类,初始化instance。
28+
29+
这种方式既保证线程安全,单例对象的唯一,也延迟了单例的初始化,推荐使用这种方式来实现单例模式。
30+
31+
[Singleton4.java](/Singleton4.java)
32+
### 5. 枚举单例
33+
默认枚举实例的创建是线程安全的,即使反序列化也不会生成新的实例,任何情况下都是一个单例。
34+
35+
优点: 简单!
36+
37+
[Singleton5.java](/Singleton5.java)
38+
### 6. 容器实现单例
39+
SingletonManager可以管理多个单例类型,使用时根据key获取对象对应类型的对象。这种方式可以通过统一的接口获取操作,隐藏了具体实现,降低了耦合度。
40+
41+
[Singleton6.java](/Singleton6.java)

‎src/review02/Singleton1.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package review02;
2+
/*
3+
* 饿汉式实现单例模式
4+
*/
5+
public class Singleton1 {
6+
private static Singleton1 instance = new Singleton1();
7+
8+
private Singleton1() {
9+
}
10+
11+
public static Singleton1 getInstance() {
12+
return instance;
13+
}
14+
}

‎src/review02/Singleton2.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package review02;
2+
/**
3+
* 懒汉式实现单例模式
4+
*/
5+
public class Singleton2 {
6+
private static Singleton2 instance;
7+
8+
private Singleton2() {
9+
}
10+
11+
// synchronized方法,多线程情况下保证单例对象唯一
12+
public static synchronized Singleton2 getInstance() {
13+
if (instance == null) {
14+
instance = new Singleton2();
15+
}
16+
return instance;
17+
}
18+
}

‎src/review02/Singleton3.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package review02;
2+
3+
/**
4+
* DCL实现单例模式
5+
*/
6+
public class Singleton3 {
7+
private static Singleton3 instance = null;
8+
9+
private Singleton3() {
10+
}
11+
12+
public static Singleton3 getInstance() {
13+
// 两层判空,第一层是为了避免不必要的同步
14+
// 第二层是为了在null的情况下创建实例
15+
if (instance == null) {
16+
synchronized (Singleton3.class) {
17+
if (instance == null) {
18+
instance = new Singleton3();
19+
}
20+
}
21+
22+
}
23+
return instance;
24+
}
25+
}

‎src/review02/Singleton4.java

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package review02;
2+
3+
/**
4+
* 静态内部类实现单例模式
5+
*/
6+
public class Singleton4 {
7+
private Singleton4() {
8+
}
9+
10+
public static Singleton4 getInstance() {
11+
return SingletonHolder.instance;
12+
}
13+
14+
/**
15+
* 静态内部类
16+
*/
17+
private static class SingletonHolder {
18+
private static Singleton4 instance = new Singleton4();
19+
}
20+
}

‎src/review02/Singleton5.java

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package review02;
2+
3+
/**
4+
* 枚举实现单例模式
5+
*/
6+
public enum Singleton5 {
7+
INSTANCE;
8+
public void doSomething() {
9+
System.out.println("do something");
10+
}
11+
}

‎src/review02/Singleton6.java

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package review02;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
/**
6+
* 容器类实现单例模式
7+
*/
8+
public class Singleton6 {
9+
private static Map<String, Object> objMap = new HashMap<String, Object>();
10+
11+
public static void regsiterService(String key, Object instance) {
12+
if (!objMap.containsKey(key)) {
13+
objMap.put(key, instance);
14+
}
15+
}
16+
17+
public static Object getService(String key) {
18+
return objMap.get(key);
19+
}
20+
}

‎src/review03/FindNumber.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package review03;
2+
3+
public class FindNumber {
4+
5+
public static void main(String[] args) {
6+
int array[] = { 1, 2, 8, 9, 2, 4, 9, 12, 4, 7, 10, 13, 6, 8, 11, 15 };
7+
boolean f = find(array, 4, 4, 15);
8+
System.out.println(f);
9+
}
10+
11+
/*
12+
* 查找函数,在输入的rows行,columns列的 array数组中进行查找 当找到输入的number时,返回true,否则返回false
13+
*/
14+
static boolean find(int array[], int rows, int columns, int number) {
15+
boolean found = false;
16+
if (array != null && rows > 0 && columns > 0) {
17+
int r = 0;
18+
int c = columns - 1; // (r,c)表示矩阵数组的右上角数的下标
19+
while (r < rows && c >= 0) {
20+
if (number == array[r * columns + c]) {
21+
found = true;
22+
break;
23+
} else if (number < array[r * columns + c])
24+
c--;
25+
else
26+
r++;
27+
}
28+
}
29+
return found;
30+
}
31+
}

‎src/review03/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
![](https://i.imgur.com/BYFaxDc.png)
2+
3+
![](https://i.imgur.com/8nscOWZ.png)
4+
5+
[FindNumber.java](/FindNumber.java)
6+
7+
![](https://i.imgur.com/fxNhcTf.png)
8+
9+

‎src/review04/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
![](https://i.imgur.com/f4elf8I.png)
2+
3+
[ReplaceString.java](/ReplaceString.java)

‎src/review04/ReplaceString.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package review04;
2+
3+
public class ReplaceString {
4+
5+
public static void main(String[] args) {
6+
String str = "we are happy.";
7+
String rep = replace(str);
8+
System.out.println(rep);
9+
}
10+
11+
/*
12+
* 替换函数,将空格替换为%20
13+
*/
14+
public static String replace(String s) {
15+
String str = s;
16+
str = str.replace(" ", "%20");
17+
return str;
18+
}
19+
}

0 commit comments

Comments
 (0)
Please sign in to comment.