Accepted C Solution treat matrix as an array


  • 0
    R
    bool searchMatrix2(int** matrix, int matrixRowSize, int matrixColSize, int target) {
    	if(0==matrixRowSize) return false;
    
    	int p1=0;
    	int p2=matrixRowSize*matrixColSize;
    
    	while(p1<p2){
    		int mid = (p1+p2)/2;
    		int v=matrix[mid/matrixColSize][mid%matrixColSize];
    		if(v==target)
    			return true;
    
    		if(target>v){
    			p1=mid+1;
    		}else{
    			p2=mid;
    		}
    	}
    	return false;
    }
    

    well, the basic idea is to treat the 2d matrix as an array.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.