劍指Offer筆記(一)

劍指offer筆記(JAVA實現)

1 面試流程

1.1 star法則

  • S: 簡單的項目背景,比如項目的規模,開發軟件的功能和目標用戶
  • T: 自己完成的任務,瞭解好“參與”與“負責”
  • A: 爲了完成任務,自己做了哪些工作,是怎麼做的。可以介紹特點,平臺,技術。
  • R: 寫自己的貢獻,比如完成任務的多少,時長,修改了多少bug,做了什麼優化。

1.2 面試官喜歡針對項目問的問題

  • 你在項目中遇到的最大的問題是什麼,你是怎麼解決的
  • 在這個項目中,你學到了什麼。
  • 什麼時候會和其他成員有什麼衝突,你是怎麼解決的。

1.3 面試題

1.3.1 找出一個鏈表的倒數第k個數

package Chapter1;

class Node{
    private int value;
    public Node next;
    public void setValue(int value){
        this.value = value;
    }
    public int getValue(){
        return this.value;
    }
}
public class TheKNumInLast {
    public static Node createList(int num){
        if(num == 0){
            return null;
        }
        Node head = new Node();
        //切記,只有指針指向對內存才能存在說 head與now同時指向一個內存
        //如果head = null; now = head;now = new Node()
        //這樣head與now並不能指向同一塊內存,head還是=null
        Node now = head;
        for(int i=1; i<num; i++){
            now.setValue(i);
            now.next = new Node();
            now = now.next;
        }
        now.setValue(num);
        return head;
    }

    public static int printList(Node head){
        int count = 0;
        if(head == null){
            System.out.println("this list is empty !");
            return count;
        }
        while(true){
            count ++;
            System.out.println(head.getValue());
            if(head.next == null){
                break;
            }else{
                head = head.next;
            }
        }
        return count;
    }

    public static int findK(int k, Node head) throws Exception{
        int kValue = 0;
        if(head == null){
            throw new Exception("傳入的鏈表不能爲空");
        }
        if(printList(head) < k){
            throw new Exception("傳入的鏈表長度不足K");
        }
        Node pre = head;
        Node aft = head;
        for(int i = 1; i < k; i++){
            aft = aft.next;
        }
        while(aft.next != null){
            pre = pre.next;
            aft = aft.next;
        }
        kValue = pre.getValue();
        return kValue;
    }

    public static void main(String[] args) {
        Node head = createList(15);
        System.out.println(printList(head));
        try {
            System.out.println("the last k max = "+findK(5, head));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

1.3.2 四種實現singleton模式

//懶漢模式
class SingletonL{
    private static SingletonL a;
    public static SingletonL getSingletonL(){
        a = new SingletonL();
        return a;
    }
}
//惡漢模式
class SingletonE{
    private static SingletonE a = new SingletonE();
    public static SingletonE getSingletonE(){
        return a;
    }
}
//雙重檢查模式
class SingletonS{
    private static SingletonS a;
    public static SingletonS getSingletonS(){
        if(a == null){
            synchronized(SingletonE.class){
                if(a == null){
                    a = new SingletonS();
                }
            }
        }
        return a;
    }
}
//使用內部類
class SingletonN{
    private static class Neibu{
        public static SingletonN a = new SingletonN();
    }
    public static SingletonN getSingletonN(){
        return Neibu.a;
    }
}

1.3.3 在二維有序數組裏查找

| 1 2 8 9     |
| 2 4 9 12    |
| 4 7 10 13   |
| 6 8 11 15   |
| 12 17 19 20 |

在其中查找 7
package Chapter2;

public class FindNumIn2 {
    public static int[] findNum(int[][] a, int key){
        int[] pos = {-1,-1};
        if(a == null || a.length == 0){
            return pos;
        }

        int lie = a.length;
        int hang = a[0].length;
        int i = hang -1;
        int j = 0;
        for(; i >= 0 && j < lie; ){
            if(key < a[j][i]){
                i--;
                continue;
            }
            if(key > a[j][i]){
                j++;
                continue;
            }
            if(key == a[j][i]){
                pos[0] = j+1;
                pos[1] = i+1;
                break;
            }
        }

        return pos;
    }
    public static void main(String[] args) {
        int[][] a = {
                {1,2,8,9},
                {2,4,9,12},
                {4,7,10,13},
                {6,8,11,15},
                {12,17,19,20}
        };
        int[] pos = findNum(a, 7);
        System.out.println("("+pos[0]+" , "+pos[1]+")");
    }
}

博客地址遷移至 int32.me

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章