标签:stringbu lin 注意 加锁 first rgs false arc private
package Problem2;
//单例模式实现,注意两次判断,一次加锁
public class SingletonClass {
private static volatile SingletonClass instance;
private SingletonClass(){
}
public static SingletonClass getInstance(){
if(instance == null){
synchronized (SingletonClass.class) {
if(instance == null){
instance = new SingletonClass();
}
}
}
return instance;
}
}
package Problem3;
public class ArraySearch {
public static boolean find(int arr[][],int keyValue){
int column = arr.length-1;
int row = 0;
while(column>=0 && row<arr.length){
if(arr[row][column] == keyValue){
return true;
}
if(arr[row][column] > keyValue){
column--;
}
else{
row++;
}
}
return false;
}
public static void main(String[] args) {
int[][] array = new int[4][4];
array[0][0] = 1;
array[0][1]=2;
array[0][2]=8;
array[0][3]=9;
array[1][0]=2;
array[1][1]=4;
array[1][2]=9;
array[1][3]=12;
array[2][0]=4;
array[2][1]=7;
array[2][2]=10;
array[2][3]=13;
array[3][0]=6;
array[3][1]=8;
array[3][2]=11;
array[3][3]=15;
System.out.println(find(array,7));
System.out.println(find(array,5));
}
}
package Problem4;
public class Replace {
public String replace(String input){
StringBuilder builder = new StringBuilder();
if(input == null|| input.length()==0){
return null;
}
for(int i = 0; i < input.length();i++){
if(input.charAt(i) == ‘ ‘){
builder.append(‘%‘);
builder.append(‘2‘);
builder.append(‘0‘);
}
else{
builder.append(input.charAt(i));
}
}
return builder.toString();
}
public static void main(String[] args) {
Replace r = new Replace();
String s = "We are happy.";
System.out.println(r.replace(s));
}
}
package Problem5;
import java.util.Stack;
class LinkNode{
LinkNode next;
int nodevalue;
}
public class ReversePrint {
public void PrintReverse(LinkNode firstnode){
Stack<LinkNode> stack = new Stack<LinkNode>();
while(firstnode!=null){
stack.push(firstnode);
firstnode = firstnode.next;
}
while(!stack.isEmpty()){
System.out.print(stack.pop().nodevalue +" ");
}
System.out.println();
}
public static void main(String[] args) {
ReversePrint r = new ReversePrint();
LinkNode node1 = new LinkNode();
LinkNode node2 = new LinkNode();
LinkNode node3 = new LinkNode();
node1.nodevalue = 1;
node2.nodevalue = 2;
node3.nodevalue = 3;
node1.next = node2;
node2.next = node3;
r.PrintReverse(node1);
}
}
剑指offer(Problem2-Problem5)(java)
标签:stringbu lin 注意 加锁 first rgs false arc private
原文地址:https://www.cnblogs.com/happylee-go/p/8908875.html