Sword Point Offer (PROBLEM2-PROBLEM5) (Java)

Source: Internet
Author: User

Package Problem2;
Single-case mode implementation, note two judgments, one lock
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);
}

}

Sword Point Offer (PROBLEM2-PROBLEM5) (Java)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.