国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Lintcode: Search a 2D matrix II

2019-11-14 23:35:37
字體:
供稿:網(wǎng)友
Lintcode: Search a 2D matrix II
Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of it.This matrix has the following PRoperties:    * Integers in each row are sorted from left to right.    * Integers in each column are sorted from up to bottom.    * No duplicate integers in each row or column.ExampleConsider the following matrix:[    [1, 3, 5, 7],    [2, 4, 7, 8],    [3, 5, 9, 10]]Given target = 3, return 2.ChallengeO(m+n) time and O(1) extra space

很巧妙的思路,可以從左下或者右上開始找

 1 public class Solution { 2     /** 3      * @param matrix: A list of lists of integers 4      * @param: A number you want to search in the matrix 5      * @return: An integer indicate the occurrence of target in the given matrix 6      */ 7     public int searchMatrix(int[][] matrix, int target) { 8         // write your code here 9         if (matrix==null || matrix.length==0 || matrix[0].length==0) return 0;10         int m = matrix.length;11         int n = matrix[0].length;12         int count = 0;13         int row = m-1;14         int col = 0;15         while (row>=0 && row<m && col>=0 && col<n) {16             int cur = matrix[row][col];17             if (cur == target) {18                 count++;19                 col++;20                 row--;21             }22             else if (cur > target) {23                 row--;24             }25             else col++;26         }27         return count;28     }29 }


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 黑山县| 英德市| 顺平县| 昆山市| 辽阳县| 河源市| 于都县| 长泰县| 治多县| 大田县| 延津县| 清涧县| 榆树市| 新昌县| 云霄县| 耒阳市| 中牟县| 丰县| 集安市| 巴塘县| 南丰县| 齐齐哈尔市| 叶城县| 威海市| 乌苏市| 饶阳县| 漠河县| 宜城市| 福建省| 隆林| 武城县| 庆城县| 库伦旗| 昭觉县| 会同县| 新乡市| 时尚| 兴宁市| 寻甸| 泸溪县| 广宁县|