LeetCode中怎么查找二維數(shù)組

今天就跟大家聊聊有關(guān)LeetCode中怎么查找二維數(shù)組,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

十載的望奎網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。全網(wǎng)整合營(yíng)銷推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整望奎建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)公司從事“望奎網(wǎng)站設(shè)計(jì)”,“望奎網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。


1,問題簡(jiǎn)述

在一個(gè) n * m 的二維數(shù)組中,每一行都按照從左到右遞增的順序排序,每一列都按照從上到下遞增的順序排序。請(qǐng)完成一個(gè)函數(shù),輸入這樣的一個(gè)二維數(shù)組和一個(gè)整數(shù),判斷數(shù)組中是否含有該整數(shù)。

2,示例

現(xiàn)有矩陣 matrix 如下:
[  [1,   4,  7, 11, 15],  [2,   5,  8, 12, 19],  [3,   6,  9, 16, 22],  [10, 13, 14, 17, 24],  [18, 21, 23, 26, 30]]給定 target = 5,返回 true。
給定 target = 20,返回 false。
 
限制:
0 <= n <= 1000
0 <= m <= 1000

3,題解思路

本題基于二維數(shù)組給出的特點(diǎn)和hashSet兩種思路進(jìn)行解決,那么接下來看下題解程序是怎么個(gè)實(shí)現(xiàn)方式吧

4,題解程序


import java.util.HashSet;
public class FindNumberIn2DArrayTest {    public static void main(String[] args) {        int[][] matrix = {                {1, 4, 7, 11, 15},                {2, 5, 8, 12, 19},                {3, 6, 9, 16, 22},                {10, 13, 14, 17, 24},                {18, 21, 23, 26, 30}        };        int target = 5;        boolean numberIn2DArray = findNumberIn2DArray(matrix, target);        System.out.println("numberIn2DArray = " + numberIn2DArray);
   }
   public static boolean findNumberIn2DArray(int[][] matrix, int target) {        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {            return false;        }        int rowLength = matrix.length;        int colLength = matrix[0].length - 1;        int row = 0;        while (row < rowLength && colLength >= 0) {            if (matrix[row][colLength] == target) {                return true;            } else if (matrix[row][colLength] > target) {                colLength--;            } else {                row++;            }        }        return false;    }
   public static boolean findNumberIn2DArray2(int[][] matrix, int target) {        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {            return false;        }        HashSet<Integer> hashSet = new HashSet<>(matrix.length * matrix[0].length);        int rowLength = matrix.length;        int colLength = matrix[0].length;        for (int i = 0; i < rowLength; i++) {            for (int j = 0; j < colLength; j++) {                if (hashSet.contains(target)) {                    return true;                }                hashSet.add(matrix[i][j]);            }        }        return hashSet.contains(target);    }}

5,題解程序圖片版

LeetCode中怎么查找二維數(shù)組

看完上述內(nèi)容,你們對(duì)LeetCode中怎么查找二維數(shù)組有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

文章名稱:LeetCode中怎么查找二維數(shù)組
當(dāng)前URL:http://muchs.cn/article38/pidhsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、網(wǎng)站排名建站公司、網(wǎng)站建設(shè)、網(wǎng)站改版、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名