题目地址:
https://www.lintcode.com/problem/204
要求实现单例模式。
可以使用饿汉模式,直接new出来,从而没有线程安全问题。代码如下:
class Solution {
private static final Solution instance = new Solution();
private Solution() {
}
/**
* @return: The same instance of this class every time
*/
public static Solution getInstance() {
// write your code here
return instance;
}
}
时空复杂度 O ( 1 ) O(1) O(1)。