03_二維數組的查找

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

思路:從一個每一行從左到右依次遞增,每一列從上到下依次遞增的二維數組查找一個元素,可以選擇從數組左上角開始查找array[i][j],如果目標元素大於array[i][j],i+=1,如果元素小於array[i][j],j-=1,依次循環直至找到這個數。

Java版本:

public class FindInPartiallySortedMatrix {

    /*從右上角開始找,這是第一種方法
    首先選取數組中右上角的數字。如果該數字等於要查找的數字,查找過程結束: 
    如果該數字大於要查找的數字,剔除這個數字所在的列:如果該數字小於要查找的數字,剔除這個數字所在的行。 
    也就是說如果要查找的數字不在數組的右上角,則每-次都在數組的查找範圍中剔除)行或者一列,這樣每一步都可以縮小查找的範圍,直到找到要查找的數字,或者查找範圍爲空。 
     */
    public static boolean Find1(int[][] matrix, int number)
    {
        boolean found = false;

        if( matrix == null)
            return found;
        else 
        {
            // matrix.length:行數
            // matrix[0].length:第0行的長度,即列數
            // matrix[1].length:第一行的長度,即列數
            int rows = matrix.length; 
            int cols = matrix[1].length;

            int row = 0;
            int col = cols -1;
            while(row < rows && col >= 0)
            {
                if(matrix[row][col] == number)
                {
                    found = true;
                    break;
                }
                else if(matrix[row][col] > number)
                    --col;
                else
                    ++row;
            }
        }   
        return found;   
    }

    //從左下角開始找,這是第二種方法 
    public static boolean Find2(int[][] matrix, int number)
    {
        boolean found = false;

        if( matrix == null)
            return found;
        else 
        {
            // matrix.length:行數
            // matrix[0].length:第0行的長度,即列數
            // matrix[1].length:第一行的長度,即列數
            int rows = matrix.length; 
            int cols = matrix[1].length;

            int row = rows-1;
            int col = 0;
            while(row >= 0 && col < cols )
            {
                if(matrix[row][col] == number)
                {
                    found = true;
                    break;
                }
                else if(matrix[row][col] < number)
                    ++col;
                else
                    --row;
            }
        }   
        return found;   
    }


    public static void main(String[] args) {
        int[][] matrix = {  
                {1, 2, 8, 9},  
                {2, 4, 9, 12},  
                {4, 7, 10, 13},  
                {6, 8, 11, 15},
                {8, 10, 15, 18} 
        };

        System.out.println("從右上角開始找(matrix, 15): "+Find1(matrix, 15));
        System.out.println("從右上角開始找(matrix, 16): "+Find1(matrix, 16));
        System.out.println("從右上角開始找(matrix, 7): "+Find1(matrix, 7));
        System.out.println("從右上角開始找(null, 18): "+Find1(null, 18));
        System.out.println();
        System.out.println("從左下角開始找(matrix, 15): "+Find2(matrix, 15));
        System.out.println("從左下角開始找(matrix, 16): "+Find2(matrix, 16));
        System.out.println("從左下角開始找(matrix, 7): "+Find2(matrix, 7));
        System.out.println("從左下角開始找(null, 18): "+Find2(null, 18));
    }
}

這裏寫圖片描述

Python版本:

# -*- coding:utf-8 -*-
# 從右上角開始找,輸出數組中target的個數
# array 二維數組
def FindInSortedArray1(array,target):
    if(array == []):
        count = 0
    else:
        rows = len(array) # 行數
        cols = len(array[0]) # 列數
        i = 0
        j = cols - 1
        count = 0
        while(i < rows and j >= 0):
            if( array[i][j] == target):
                count += 1
                j -= 1
            elif( array[i][j] > target):
                j -= 1
            else:
                i += 1
    return count

# 從左下角開始找,輸出數組中target的個數
def FindInSortedArray2(array,target):
    if(array == []):
        count = 0
    else:
        rows = len(array) # 行數
        cols = len(array[0]) # 列數
        i = rows-1
        j = 0
        count = 0
        while(i >= 0 and j < cols):
            if( array[i][j] == target):
                count += 1
                j += 1
            elif( array[i][j] < target):
                j += 1
            else:
                i -= 1
    return count

#從左下角開始找
#def FindInPartiallySortedarray2():


array = [[1, 2, 8, 9],
         [2, 4, 9, 12],
         [4, 7, 10, 13],
         [6, 8, 11, 15]]
array2 = []
array3 = [[62,63,64,65,66,67,68,69,70,71,72,73,74,75],
          [63,64,65,66,67,68,69,70,71,72,73,74,75,76],
          [64,65,66,67,68,69,70,71,72,73,74,75,76,77],
          [65,66,67,68,69,70,71,72,73,74,75,76,77,78],
          [66,67,68,69,70,71,72,73,74,75,76,77,78,79],
          [67,68,69,70,71,72,73,74,75,76,77,78,79,80]]

print(FindInSortedArray1(array,8))
print(FindInSortedArray2(array,8))
print(FindInSortedArray1(array2,8))
print(FindInSortedArray2(array2,8))
print(FindInSortedArray1(array3,67))
print(FindInSortedArray2(array3,67))

輸出:
2
2
0
0
6
6

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