Skip to content

Commit 9edae56

Browse files
committed
singleton design pattern
1 parent 433af0c commit 9edae56

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.cszjo.design.patterns.factory.singleton;
2+
3+
/**
4+
* Created by hansiming on 2017/10/19.
5+
*/
6+
public class DoubleCheckSingleton {
7+
8+
private static DoubleCheckSingleton instance = null;
9+
10+
private DoubleCheckSingleton() {
11+
// prevent instance on outer
12+
}
13+
14+
public static DoubleCheckSingleton getInstance() {
15+
if (instance == null) {
16+
synchronized (DoubleCheckSingleton.class) {
17+
if (instance == null) {
18+
instance = new DoubleCheckSingleton();
19+
}
20+
}
21+
}
22+
return instance;
23+
}
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.cszjo.design.patterns.factory.singleton;
2+
3+
/**
4+
* Created by hansiming on 2017/10/19.
5+
*/
6+
public class StaticHolderSingleton {
7+
8+
private static class SingletonHolder {
9+
private final static StaticHolderSingleton instance = new StaticHolderSingleton();
10+
}
11+
12+
public static StaticHolderSingleton getInstance() {
13+
return SingletonHolder.instance;
14+
}
15+
}

src/com/cszjo/offer/Demo10.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.cszjo.offer;
2+
3+
/**
4+
* 依旧是斐波拉契数列
5+
* 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。
6+
* 请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?
7+
* Created by hansiming on 2017/10/19.
8+
*/
9+
public class Demo10 {
10+
11+
public int RectCover(int target) {
12+
if (target <= 2)
13+
return target;
14+
15+
return RectCover(target - 1) + RectCover(target - 2);
16+
}
17+
}

src/com/cszjo/offer/Demo11.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.cszjo.offer;
2+
3+
/**
4+
* 输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。
5+
* Created by hansiming on 2017/10/19.
6+
*/
7+
public class Demo11 {
8+
9+
public int NumberOf1(int n) {
10+
11+
int count = 0;
12+
if (n < 0) {
13+
n = n & 0x7FFFFFFF;
14+
count ++;
15+
}
16+
17+
while (n > 0) {
18+
count += n & 1;
19+
n = n >> 1;
20+
}
21+
22+
return count;
23+
}
24+
}

src/com/cszjo/offer/Demo9.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.cszjo.offer;
2+
3+
/**
4+
* 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。
5+
* 求该青蛙跳上一个n级的台阶总共有多少种跳法。
6+
* Created by hansiming on 2017/10/19.
7+
*/
8+
public class Demo9 {
9+
10+
public int JumpFloorII(int target) {
11+
12+
if (target <= 2)
13+
return target;
14+
15+
return 2 * JumpFloorII(target - 1);
16+
}
17+
}

0 commit comments

Comments
 (0)