zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    ZOJ 3765 Lights (zju March I)伸展树Splay
    UVA 11922 伸展树Splay 第一题
    UVALive 4794 Sharing Chocolate DP
    ZOJ 3757 Alice and Bod 模拟
    UVALive 3983 捡垃圾的机器人 DP
    UVA 10891 SUM游戏 DP
    poj 1328 Radar Installatio【贪心】
    poj 3264 Balanced Lineup【RMQ-ST查询区间最大最小值之差 +模板应用】
    【转】RMQ-ST算法详解
    poj 3083 Children of the Candy Corn 【条件约束dfs搜索 + bfs搜索】【复习搜索题目一定要看这道题目】
  • 原文地址:https://www.cnblogs.com/qiuyong/p/6675531.html
Copyright © 2011-2022 走看看