240. Search a 2D Matrix II

240 Search a 2D Matrix II

Total Accepted: 39652 Total Submissions: 113403 Difficulty: Medium

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 in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.

For example,
Consider the following 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]
]
Given target = 5, return true.
Given target = 20, return false.

Hide Company Tags Amazon Google Apple
Hide Tags Binary Search Divide and Conquer
Hide Similar Problems (M) Search a 2D Matrix

思路:##

We start search the matrix from top right corner, initialize the current position to top right corner, if the target is greater than the value in current position, then the target can not be in entire row of current position because the row is sorted, if the target is less than the value in current position, then the target can not in the entire column because the column is sorted too. We can rule out one row or one column each time, so the time complexity is O(m+n).

Runtime: 13 ms beat 52%

public class Solution {
      /* https://leetcode.com/discuss/48852/my-concise-o-m-n-java-solution

        */
    public boolean searchMatrix(int[][] matrix, int target) {
        
        if (matrix == null || matrix.length < 1 || matrix[0].length < 1) {
            return false;
        }
        
        int row = 0, col = matrix[0].length - 1;
        while (row < matrix.length && col >= 0) {  // start from top right corner
            if (matrix[row][col] == target) {
                return true;
            } else if ( matrix[row][col] < target) { // move down
                row++;
            } else {
                col--;  // move left
            }
        }
        
        return false;
    }
        
    public boolean searchMatrix_failed(int[][] matrix, int target) {
      
        int rstart = 0, rend = matrix.length - 1;
        int cstart = 0, cend = matrix[0].length - 1;
        
        while ( cstart < cend) {
            int cmid = cstart + (cend - cstart) / 2;
            int rmid = rstart + (rend - rstart) / 2;
            if (matrix[rmid][cmid] == target) {
                return true;
            } else if (matrix[rmid][cmid] > target) {
                rend = rmid - 1;
                cend = cmid - 1;
            } else {
                rstart =rmid + 1;
                cstart = cstart + 1;
            }
        }
        return false;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末鸣奔,一起剝皮案震驚了整個濱河市柒莉,隨后出現(xiàn)的幾起案子默辨,更是在濱河造成了極大的恐慌,老刑警劉巖路操,帶你破解...
    沈念sama閱讀 218,386評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件疾渴,死亡現(xiàn)場離奇詭異,居然都是意外死亡屯仗,警方通過查閱死者的電腦和手機搞坝,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來魁袜,“玉大人桩撮,你說我怎么就攤上這事》宓” “怎么了店量?”我有些...
    開封第一講書人閱讀 164,704評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長垮卓。 經(jīng)常有香客問我垫桂,道長,這世上最難降的妖魔是什么粟按? 我笑而不...
    開封第一講書人閱讀 58,702評論 1 294
  • 正文 為了忘掉前任诬滩,我火速辦了婚禮,結(jié)果婚禮上灭将,老公的妹妹穿的比我還像新娘疼鸟。我一直安慰自己,他們只是感情好庙曙,可當我...
    茶點故事閱讀 67,716評論 6 392
  • 文/花漫 我一把揭開白布空镜。 她就那樣靜靜地躺著,像睡著了一般捌朴。 火紅的嫁衣襯著肌膚如雪吴攒。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,573評論 1 305
  • 那天砂蔽,我揣著相機與錄音洼怔,去河邊找鬼。 笑死左驾,一個胖子當著我的面吹牛镣隶,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播诡右,決...
    沈念sama閱讀 40,314評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼安岂,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了帆吻?” 一聲冷哼從身側(cè)響起域那,我...
    開封第一講書人閱讀 39,230評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎猜煮,沒想到半個月后琉雳,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體样眠,經(jīng)...
    沈念sama閱讀 45,680評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,873評論 3 336
  • 正文 我和宋清朗相戀三年翠肘,在試婚紗的時候發(fā)現(xiàn)自己被綠了檐束。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,991評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡束倍,死狀恐怖被丧,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情绪妹,我是刑警寧澤甥桂,帶...
    沈念sama閱讀 35,706評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站邮旷,受9級特大地震影響黄选,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜婶肩,卻給世界環(huán)境...
    茶點故事閱讀 41,329評論 3 330
  • 文/蒙蒙 一办陷、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧律歼,春花似錦民镜、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至畔况,卻和暖如春鲸鹦,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背跷跪。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評論 1 270
  • 我被黑心中介騙來泰國打工馋嗜, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人域庇。 一個月前我還...
    沈念sama閱讀 48,158評論 3 370
  • 正文 我出身青樓嵌戈,卻偏偏與公主長得像覆积,于是被迫代替她去往敵國和親听皿。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,941評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關LeetCode的問題, 分享了一些自己做題目的經(jīng)驗宽档。 張土汪:刷leetcod...
    土汪閱讀 12,745評論 0 33
  • Write an efficient algorithm that searches for a value in...
    matrxyz閱讀 141評論 0 0
  • 問題描述 Write an efficient algorithm that searches for a val...
    codingXue閱讀 155評論 0 0
  • 開始不再苛責自己尉姨,在每一個困了的時刻果斷的躺下睡覺,不定表吗冤,自然而然的醒來又厉,就有了每一個躺下的輕松幸福和一個個精彩...
    木木的心心閱讀 202評論 0 0
  • 過年回農(nóng)村九府,歸來淚滿巾。 田荒走野兔覆致,不見種田人侄旬。 青壯搓麻將,翁嫗帶幼孫煌妈。 兒童留守多儡羔,未見爹娘親。 偶見兩書生...
    浪花朵朵ncj閱讀 204評論 0 1