Finding 4 _ two-dimensional array to prove safety Offer / 240 Search a 2D Matrix II

** problem **

(The length of each one-dimensional array of same), each row from left to right in order of ascending sort, to sort each column in a two-dimensional array in order of increasing from top to bottom. It determines whether it contains an array of integers.
Here Insert Picture Description
** Examples **
Here Insert Picture Description
** thinking **

  1. Violence O (n ^ 2)

The two-dimensional array of all the elements taken out into the set, and see whether there is the number set in

  1. Very violent

From top left [0] [0], and the number of elements taken comparison, if greater than, or let i + 1 j + 1, then ratio, is less than, let i-1 or j-1 ratio then
Here Insert Picture Description

  1. Binary search O (nlogn)

Each line is increasing from left to right on each row binary search

  1. O (n)
  • From the bottom left corner of view, up diminishing numbers, the right number can add up, so start looking from the lower left corner, when you are looking for a large digital numbers than the lower-left corner. To find the right figure than the lower left corner of the digital hours on shift.
  • Compared with the method, each time the comparisons less a column or row, the comparison region greatly reduced
    Here Insert Picture Description
    ** ** Code
//方法3
public class Solution {
    //二分查找,递归
   public boolean binarySearch(int[] array, int low, int high,int target) {
        //low>high要放在最前面,因为后面的[low],[high]可能会有越界
        if (low>high || target<array[low] || target>array[high] ) return false;
        int mid = (low+high)/2;
        if (array[mid]>target) return binarySearch(array,low,mid-1,target);
        if (array[mid]<target) return binarySearch(array,mid+1,high,target);
        //if (array[mid]==target) 
        return true;
    }
    public boolean Find(int target, int [][] array) {
        for (int i=0; i<array.length; i++) {
            if (binarySearch(array[i],0,array[i].length-1,target)){
                return true;
            }
        }
        return false;
    }
}
//方法4
import java.util.*;
public class Solution {
    public boolean Find(int target, int [][] array) {
        int i=array.length-1,j=0;
        while (j<=array[0].length-1 && i>=0) {
        	int now= array[i][j];
            if (now==target) return true;
            if (now<target) j++;
            if (now>target) i--;
            /*
            不用if(array[i][j]<target) j++;因为如果对i和j进行了修改,影响后面的if
            否则的话,要写:
            if(arr[i][j]<target) j++;
            if (j<=array[0].length-1 && array[i][j]>target) i--;
             */
        }
        return false;
    }
}
class Solution:
    def searchMatrix(self, matrix, target):
        """
        :type matrix: List[List[int]]
        :type target: int
        :rtype: bool
        """
        if matrix==None or len(matrix)==0 or len(matrix[0])==0:
            return False
        
        i=len(matrix)-1;j=0;j_max=len(matrix[0])-1
        
        while (i>=0 and j<=j_max):
            t = matrix[i][j]
            if target==t: return True
            if target<t: i-=1
            if target>t: j+=1
        return False

** Tips **

Starting from the example, look at how you can reduce the number of operations to perform basic

Published 78 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/puspos/article/details/103925469