劍指Offer算法

1、在一個二維數組中,每一行都按照從左到右遞增的順序排序,每一列都按照從上到下遞增的順序排序。請完成一個函數,輸入這樣的一個二維數組和一個整數,判斷數組中是否含有該整數。

 public static boolean Find(int target, int [][] array) {
        int rows=array.length;
		int columns=array[0].length;
		int row=0;
		int column=columns-1;
        boolean flag=false;
		//從右上角開始尋找目標值,如果目標值大於數組中對應位置的值,row+1;如果目標值小於數組中對應的值,column-1;
		while(row<rows&&column>=0){
			if(array[row][column]==target){
				flag=true;
                break;
            }
			else if(target<array[row][column])
				column--;
			else
				row++;
		}
		return flag;
		
    }

2、輸入一個鏈表,從尾到頭打印鏈表每個節點的值。

public class Solution {
    //這道題可以先正向的將鏈表中的數據讀出來,然後放到棧中,根據棧的特點,就能將每個節點的值從尾打印到頭部
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        if(listNode==null)
        {
           return null;
        }
        ArrayList<Integer> a=new ArrayList<Integer>();
        Stack<Integer> s=new Stack<Integer>();
        ListNode temp=listNode;
        while(temp!=null){
            s.push(temp.val);
            temp=temp.next;
        }
        while(!s.empty()){
            a.add(s.pop());
        }
        return a;
        
    }
}

3、用兩個棧來實現一個隊列,完成隊列的Push和Pop操作。 隊列中的元素爲int類型

//此題主要考察棧和隊列的性質。棧先入後出,隊列先入先出。我們可以先通過stack1入棧,通過stack2出棧,這樣就能當做隊列了,如果stack2棧中沒有數據時,將stack1中數據壓入到stack2中
public class Solution {
    Stack<Integer> stack1 = new Stack<Integer>();
    Stack<Integer> stack2 = new Stack<Integer>();
    
    public void push(int node) {
        stack1.push(node);
    }
    
    public int pop() {
       if(stack2.empty()){
           while(!stack1.empty()){
               stack2.push(stack1.pop());
           }
       }
        return stack2.pop().intValue();
    }
}
4、把一個數組最開始的若干個元素搬到數組的末尾,我們稱之爲數組的旋轉。 輸入一個非遞減排序的數組的一個旋轉,輸出旋轉數組的最小元素。 例如數組{3,4,5,1,2}爲{1,2,3,4,5}的一個旋轉,該數組的最小值爲1。 NOTE:給出的所有元素都大於0,若數組大小爲0,請返回0。
public class Solution {
    //這道題也就是找出最小值
    public int minNumberInRotateArray(int [] array) {
        if(array.length==0)
            return 0;
        int min=array[0];
        for(int i=0;i<array.length;++i){
          if(min>array[i])
              min=array[i];
        }
        return min;
    }
}
5、大家都知道斐波那契數列,現在要求輸入一個整數n,請你輸出斐波那契數列的第n項。

n<=39

  public int Fibonacci(int n) {//f(0)=0,f(1)=1,f(2)=1,f(n)=f(n-1)+f(n-2)
        if(n==1)
            return 1;
        if(n==0)
            return 0;
        else{
           return Fibonacci(n-1)+Fibonacci(n-2);
        }
         
        
    }

6、一隻青蛙一次可以跳上1級臺階,也可以跳上2級。求該青蛙跳上一個n級的臺階總共有多少種跳法。

  public int JumpFloor(int target) {
        //對於青蛙來說,跳n級臺階,第一跳,分爲兩種情況:跳1級臺階;跳2級臺階,這樣就有f(n)=f(n-1)+f(n-2);
        if(target==1||target==2)
            return target;
        return JumpFloor(target-1)+JumpFloor(target-2);
    }

7、一隻青蛙一次可以跳上1級臺階,也可以跳上2級……它也可以跳上n級。求該青蛙跳上一個n級的臺階總共有多少種跳法。

public int JumpFloorII(int target) {
        //f(n)=f(n-1)(相當於第一次跳了1級臺階)+f(n-2)(相當於第一次跳了2級臺階)+f(n-3)+....f(n-(n-1))+f(n-n)=f(0)+f(1)+..f(n-1)=2*f(n-1)
        if(target==1)
            return 1;
        if(target==2)
            return 2;
        return 2*JumpFloorII(target-1);
    }
8、我們可以用2*1的小矩形橫着或者豎着去覆蓋更大的矩形。請問用n個2*1的小矩形無重疊地覆蓋一個2*n的大矩形,總共有多少種方法?
 public int RectCover(int target) {
        //其實這道題和青蛙跳臺階一樣。因爲大矩形2*n,橫着放只能放一個,豎着放能放兩個,對於要填滿矩形來說,要麼先橫着放,則還有f(n-1),要麼豎着放,則還有f(n-2)
        if(target<=2)
            return target;
        return RectCover(target-1)+RectCover(target-2);
    }
9、輸入一個整數數組,實現一個函數來調整該數組中數字的順序,使得所有的奇數位於數組的前半部分,所有的偶數位於位於數組的後半部分,並保證奇數和奇數,偶數和偶數之間的相對位置不變。
public class Solution {
    //這道題的解法:將奇數放到數組前面,將偶數放到數組後面,可以採用冒泡的形式,如果兩個相鄰的數,左邊是偶數,右邊是奇數,則調換位置;
    //如果左邊是奇數,右邊是偶數,不用調換位置
    public void reOrderArray(int [] array) {
        for(int i=0;i<array.length-1;++i){//外層控制遍數
            for(int j=array.length-1;j>i;j--){//內層進行比較,調換
                if(array[j]%2!=0&&array[j-1]%2==0){
                    int temp=array[j];
                    array[j]=array[j-1];
                    array[j-1]=temp;
                
                }
            }
           
        }
    }
}

10、輸入一個鏈表,輸出該鏈表中倒數第k個結點

public class Solution {
    public ListNode FindKthToTail(ListNode head,int k) {
        if(head==null){  
                return null;  
            }  
            int count=1;  
            ListNode old=head;  
            while(head.next!=null){  
                head=head.next;  
                count++;  
            }  
            if(k>count){  
                return null;  
            }  
            for(int i=0;i<count-k;i++){  
                old=old.next;  
            }  
            return old;  
    }
}




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