Java实现-每天三道剑指Offre(2-4)

实现一个单例模式

 1 /**
 2  * 面试题2:实现单例模式
 3  * 
 4  * @author qiuyong 饿汉式
 5  */
 6 public class Singleton01 {
 7     private Singleton01() {
 8     }
 9 
10     private static Singleton01 instance = new Singleton01();
11 
12     public static Singleton01 getInstance() {
13         return instance;
14     
15     }
16 }
17 
18 /**
19  * 面试题2:懒汉式
20  * @author qiuyong
21  */
22 public class Singleton02 {
23     
24     public static Singleton02 instance;
25     
26     public synchronized static Singleton02 getInstance(){
27         if(null==instance){
28             instance=new Singleton02();
29         }        
30         return instance;
31     }
32 
33 }
34 
35 /**
36  * 面试题2:利用内部类
37  * @author qiuyong
38  */
39 public class Singleton03 {
40     
41     public static Singleton03 getInstance(){
42         return  InnerClass.instatnce;
43     }
44     
45     private static class InnerClass{
46         public static Singleton03 instatnce=new Singleton03();
47     }
48 }

二维数组的查找

  题目描述:一个二维数组,每一行从左到右递增,每一列从上到下递增.输入一个二维数组和一个整数,判断数组中是否含有整数。

public class Test {
    public static boolean find(int [][] arry,int num){
        int column=arry[0].length-1;
        int row=0;
        while(row<arry.length&&column>=0){
            if(num==arry[row][column]){
                return true;
            }
            if(arry[row][column]>num){
                column--;
            }else{
                row++;
            }
    }
    </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
}

</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) {
    </span><span style="color: #0000ff;">int</span> [][] arry=<span style="color: #0000ff;">new</span> <span style="color: #0000ff;">int</span>[3][3<span style="color: #000000;">];
    </span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> i=0;i&lt;arry.length;i++<span style="color: #000000;">){
        </span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> j=0;j&lt;arry[0].length;j++<span style="color: #000000;">){
            arry[i][j]</span>=(i+1)*(j+1<span style="color: #000000;">);
        }
    }
    
    System.out.println(</span>"查看是否有元素9:"+find(arry,9<span style="color: #000000;">));
    System.out.println(</span>"查看是否有元素9:"+find(arry,0<span style="color: #000000;">));
}

}

替换空格

  题目描述:请实现一个函数,把字符串中的每个空格替换成“%20”。

 1 public class ReplaceBlank {
 2     public static void main(String[] args) {
 3         String str="i am happy";
 4         str=replaceBlank(str);
 5         System.out.println(str);
 6     }
 7     
 8     
 9     public static String replaceBlank(String str){
10         if(null==str){
11             return null;
12         }
13         StringBuffer sb=new StringBuffer();
14         for(int i=0;i<str.length();i++){
15             if(str.charAt(i)==' '){
16                 sb.append("%20");
17             }else{
18                 sb.append(str.charAt(i));
19             }
20         }
21         return sb.toString();
22     }
23 
24 }
原文地址:https://www.cnblogs.com/qiuyong/p/6675531.html