搜索二维矩阵

原题

  Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
  Integers in each row are sorted from left to right.
  The first integer of each row is greater than the last integer of the previous row.
  For example,
  Consider the following matrix:  Given target = 3, return true.算法

[
  [1,   3,  5,  7],
  [10, 11, 16, 20],
  [23, 30, 34, 50]
]

 

 

题目大意

  给定一个二维矩阵,实现一个算法在矩阵中实现快速搜索。即给定k,在矩阵中搜索k
  矩阵中下面的性质:每一行每一列都是排好序的,每一行的第一个数都比上一行的最后一个数大。数组

解题思路

  解法一:先用二叉查看找算法找到数字所在的列,再用二叉查找算法找数字所在的列。找到就返回true,不然返回false。ide

代码实现

算法实现类spa

public class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {

        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return false;
        }

        int row = matrix.length;
        int column = matrix[0].length;
        int low = 0;
        int high = row - 1;
        int mid = 0;

        // 找结果所在的列
        while (low <= high) {
            mid = low + (high - low) / 2;

            if (target < matrix[mid][column - 1]) {
                high = mid - 1;
            } else if (target > matrix[mid][column - 1]) {
                low = mid + 1;
            } else {
                return true;
            }
        }

        // 决定列所在的最终位置
        int targetRow = mid;
        if (matrix[mid][column - 1] < target) {
            targetRow++;
        }

        // 目标列超出,无结果
        if (targetRow >= row) {
            return false;
        }

        low = 0;
        high = column - 1;
        // 找所在的行,找到返回true,没有返回false
        while (low <= high) {
            mid = low + (high - low) / 2;

            if (target < matrix[targetRow][mid]) {
                high = mid - 1;
            } else if (target > matrix[targetRow][mid]) {
                low = mid + 1;
            } else {
                return true;
            }
        }

        return false;
    }
}

 

解法二:.net

\

解题思路:
首先,咱们选择查找数子7为例来一步步分析查找的过程。
而后,咱们选取数组右上角的9。
code

代码实现:ci

package array;

public class QuencyArray {

    public static boolean FindArray(int[][] arr,int number){
        int rows = arr.length;
        int columns = arr[0].length;
        boolean flag = false;
        if(arr!=null && rows>0 && columns>0){
            int row = 0;
            int col = columns-1;
            while(row0){
                if(arr[row][col]number){//说明待查找的数在左方
                    col--;
                }else {
                    flag = true;
                    break;
                }
            }
        }
        return flag;
    }

    public static void main(String[] args) {
        int[][] arr = {
            {1,2,8,9},
            {2,4,9,12},
            {4,7,10,13},
            {6,8,11,15},
            {8,10,14,17}
        };
        System.out.println(FindArray(arr,8));//true
        System.out.println(FindArray(arr,22));//false
    }
}

第一次查找咱们选取的是数组右上角进行查询,一样咱们能够选取数组的左下角。 或者:get

  1. package com.ynu.www.offer;    
  2.     
  3. public class FindFromMatrix {    
  4.     
  5.     private static int[][] sample = new int[][] { { 1, 2, 8, 9 },    
  6.             { 2, 4, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };    
  7.     
  8.     public static void printSample() {    
  9.         for (int i = 0; i < sample.length; i++) {    
  10.             for (int j = 0; j < sample[i].length; j++) {    
  11.                 System.out.print(sample[i][j] + " ");    
  12.             }    
  13.             System.out.println();    
  14.         }    
  15.     }    
  16.     
  17.     public static boolean getValuefromMatrix(int[][] sample, int rows,    
  18.             int columns, int num) {    
  19.         boolean found = false;    
  20.         if (sample != null && rows > 0 && columns > 0) {    
  21.             int row = 0;    
  22.             int column = columns - 1;    
  23.             while (row < rows && column >= 0) {    
  24.                 int tempValue = sample[row][column];    
  25.                 if (num > tempValue) {    
  26.                     ++row;    
  27.                 } else if (num < tempValue) {    
  28.                     --column;    
  29.                 } else {    
  30.                     found = true;    
  31.                     break;    
  32.                 }    
  33.             }    
  34.         }    
  35.     
  36.         return found;    
  37.     }    
  38.     
  39.     public static void main(String[] args) {    
  40.         printSample();    
  41.         System.out.println(getValuefromMatrix(sample, 4, 4, 7));    
  42.     
  43.     }    
  44. }  
相关文章
相关标签/搜索