74. Search a 2D Matrix

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:
[ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ]
Given target = 3, returntrue.

public class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        int row = matrix.length,col = matrix[0].length;
        int i = 0,j = col-1;
        while(i<row&&j>=0)
        {
            if(target>matrix[i][j])
               i++;
            else if(target<matrix[i][j])
               j--;
            else
               return true;
        }
        return false;
        
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 清晨,獨自行走,耳畔回想起“春有百花秋有月,夏有涼風(fēng)冬有雪,若無閑事掛心頭,便是人生好時節(jié)”一切都近乎是苦盡甘來,...
    往事隨風(fēng)夢余生閱讀 673評論 1 2
  • 高考結(jié)束后絕對是人生最放松的階段,休息了三個月已經(jīng)可以把人歇懶了。 在沒有離開那所在我看來極度無趣的高中時,我厭惡...
    狐肆月閱讀 441評論 0 1
  • 一個人有多不正經(jīng),就有多深情,對他的喜歡始于穿透人心的情歌,陷于其出眾的才華,忠于其高尚的人品。 做音樂,他一直是...
    請叫我劉小百閱讀 499評論 4 6
  • "咪蒙"公眾號 2017.03.29 原標(biāo)題"有錢人終成眷屬" 我來改標(biāo)題 1.為什么有情人終究不能成眷屬? 2....
    永利哥閱讀 211評論 0 2

友情鏈接更多精彩內(nèi)容