8000 Update 剑指offer题解.md · CyC2018/CS-Notes@a968cdf · GitHub
[go: up one dir, main page]

Skip to content

Commit a968cdf

Browse files
authored
Update 剑指offer题解.md
1 parent 80147df commit a968cdf

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

剑指offer题解.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public class Singleton {
103103

104104
只需要对 getUniqueInstance() 方法加锁,就能让该方法一次只能一个线程访问,从而避免了对 uniqueInstance 变量进行多次实例化的问题。但是这样有一个问题是一次只能一个线程进入,性能上会有一定的浪费。
105105

106-
```
106+
```java
107107
public static synchronized Singleton getUniqueInstance() {
108108
if (uniqueInstance == null) {
109109
uniqueInstance = new Singleton();
@@ -123,7 +123,7 @@ private static Singleton uniqueInstance = new Singleton();
123123

124124
考虑第一个解决方案,它是直接对 getUniqueInstance() 方法进行加锁,而实际上只需要对 uniqueInstance = new Singleton(); 这条语句加锁即可。使用两个条件语句来判断 uniqueInstance 是否已经实例化,如果没有实例化才需要加锁。
125125

126-
```
126+
```java
127127
public class Singleton {
128128
private volatile static Singleton uniqueInstance;
129129
private Singleton() {
@@ -1252,7 +1252,7 @@ private boolean less(int v, int w) {
12521252

12531253
如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。
12541254

1255-
```
1255+
```java
12561256
private PriorityQueue<Integer> maxHeap = new PriorityQueue<>((o1, o2) -> o2-o1); // 实现左边部分
12571257
private PriorityQueue<Integer> minHeep = new PriorityQueue<>(); // 实现右边部分,右边部分所有元素大于左边部分
12581258
private int cnt = 0;

0 commit comments

Comments
 (0)
0