1. 程式人生 > >劍指offer——(1)二維陣列的查詢

劍指offer——(1)二維陣列的查詢

自從一年前資料結構之後,很久沒做這類題了,最好能每天一道!

三個思路吧 

public class Solution {
/*
    思路1:看錯題目,以為用一維陣列儲存就可以直接用二分查詢。。就當做複習快排了,然而現實
    是我的快排(註釋部分)在牛客網上超時了 超時了... 於是我隨便找了個快排演算法,就行! 
*/
    boolean boo = false;
    public boolean Find(int target, int [][] array) {   
        int k=0,arr[] = new int[array[0].length*array.length];       
        for(int i=0;i<array.length;i++){
            for(int j=0;j<array[0].length;j++){
                arr[k] = array[i][j];              
                k++;
            }
        }
        
        others_QS(arr,0);
        
        int start = 0,end = k-1;
        int temp = end;
        while(start<=end&&start<k&&end>=0){            
            if(target==arr[temp]){
                boo = true;  
                break;
            } 
            if(target<arr[temp]){
                end = end-1;               
            }
            if(target>arr[temp]){
                start = start+1;
            }     
            temp = end;
        }
        if(boo==true) return true;
        else return boo;   
    
    }
    
    void others_QS(int[] arr,int l) {
        if(l >= arr.length) return;
        int j = l + 1;
        for (int i = l; i < arr.length; i++) {
            if(j >= arr.length) break;
            if(arr[i] < arr[j]){
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
            j++;
        }
        if(j <= arr.length){            
            int temp2 = arr[l];
            arr[l] = arr[j-1];
            arr[j-1] = temp2;
        }
        others_QS(arr, ++l);
    }     
   
/*
     void my_QS(int x,int y){
		if(x>=y) return;
		int i = x,j = y;
		while(i<=j){
			while(i<=j && arr[i]<=arr[x]) i++;
			while(i<=j && arr[j]>arr[x]) j--;
			if(i<=j){
				int temp = arr[i];
				arr[i] = arr[j];
				arr[j] = temp;
				i++;j--;	
				}
			else {
				int temp = arr[j];
				arr[j] = arr[x];
				arr[x] = temp;
			}
		}
		QSort(0,j-1);
		QSort(i,y);
	}
*/

}
public class Solution {
/*
    思路2:暴力迴圈之。。
*/
    public boolean Find(int target, int [][] array) {
        boolean boo = false;
        for(int i=0;i<array.length;i++){
            for(int j=0;j<array[0].length;j++){
                if(target==array[i][j]){
                    boo = true;
                }
            }
        }
        if(boo==true) return true;
        else return boo;
    }
}
public class Solution {  
/*
    思路3:二維陣列每行從左到右且每列從上到下排好序,所以我們選擇從二維陣列的左下角
    array[array.length-1][0]開始查詢,如果要找的數字小於array[array.length-1][0],
    則縱座標上移;如果要找的數字大於array[array.length-1][0],則橫座標右移。
*/   
    public boolean Find(int target, int [][] array) {          
        for(int i = array.length-1,j=0;i>=0&&j<array[0].length;){
            if(target<array[i][j]){
                i--;
                continue;
            }
            if(target==array[i][j]){
                return true;
            } 
            if(target>array[i][j]){
                j++;
                continue;
            }
        }
        return false;
       }
}