LintCode: Search A 2d Matrix
生活随笔
收集整理的這篇文章主要介紹了
LintCode: Search A 2d Matrix
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.
設(shè)查找的數(shù)位y,第一行最后一列的數(shù)位x
如果x<y,x是第一行最大的,所以第一行都小于y,刪除第一行;
如果x>y,x是最后一列最小的,所以最后一列都大于y,刪除最后一列;
這樣保證x永遠(yuǎn)在可能有解的矩陣的第一行,最后一列。
時間復(fù)雜度:O(m+n)
1 class Solution { 2 public: 3 /** 4 * @param matrix, a list of lists of integers 5 * @param target, an integer 6 * @return a boolean, indicate whether matrix contains target 7 */ 8 bool searchMatrix(vector<vector<int> > &matrix, int target) { 9 // write your code here 10 int m = matrix.size(); 11 if (m == 0) { 12 return false; 13 } 14 int n = matrix[0].size(); 15 int r = 0, c = n - 1; 16 while (r < m && c >= 0) { 17 if (matrix[r][c] < target) { 18 r++; 19 } else if (matrix[r][c] > target) { 20 c--; 21 } else { 22 return true; 23 } 24 } 25 return false; 26 } 27 };?
2. 分治法1
設(shè)查找的數(shù)位y,取中心點(diǎn)x,把矩陣分解成4部分
如果x<y,x是A中最大的,所以A都小于y,刪除A;
如果x>y,x是D中最小的,所以D都小于y,刪除D;
A ?| B
————
C ?| D
時間復(fù)雜度:O(N) = O(N/4)+O(N/2)+O(1),?介于O(N^0.5)~O(N)之間
1 class Solution { 2 public: 3 /** 4 * @param matrix, a list of lists of integers 5 * @param target, an integer 6 * @return a boolean, indicate whether matrix contains target 7 */ 8 bool helper(vector<vector<int> > &M, int x1, int y1, int x2, int y2, int target) { 9 if (x1 > x2 || y1 > y2) {//empty matrix 10 return false; 11 } 12 int midx = (x1 + x2)>>1; 13 int midy = (y1 + y2)>>1; 14 if (M[midx][midy] == target) { 15 return true; 16 } 17 return (M[midx][midy] > target)? 18 (helper(M, x1, y1, x2, midy-1, target)||helper(M, x1, midy, midx-1, y2, target)): 19 (helper(M, x1, midy+1, x2, y2, target)||helper(M, midx+1, y1, x2, midy, target)); 20 21 } 22 bool searchMatrix(vector<vector<int> > &matrix, int target) { 23 // write your code here 24 int m = matrix.size(); 25 if (m == 0) { 26 return false; 27 } 28 int n = matrix[0].size(); 29 return helper(matrix, 0, 0, m - 1, n - 1, target); 30 } 31 };?
3. 分治法2?
設(shè)查找的數(shù)為y,在中線找到這樣兩個數(shù)x1,x2,使得x1<y,x2>y,把矩陣分成4部分
A| ? ? B
————
C| ? ?D
x1是A中最大的,所以A都小于y,刪掉A;
x2是D中最小的,所以D都大于y,刪掉D;
時間復(fù)雜度:O(N)=2O(N/4)+O(logn), 為O(N^0.5)?
1 class Solution { 2 public: 3 /** 4 * @param A, a list of integers 5 * @param left, an integer 6 * @param right, an integer 7 * @param target, an integer 8 * @return an integer, indicate the index of the last number less than or equal to target in A 9 */ 10 int binarySearch(vector<int> &A, int left, int right, int target) { 11 while (left <= right) {//not an empty list 12 int mid = (left + right) >> 1; 13 if (A[mid] <= target) { 14 left = mid + 1;//left is in the integer after the last integer less than or equal to target 15 } else { 16 right = mid - 1; 17 } 18 } 19 return left - 1; 20 } 21 /** 22 * @param M, a list of lists of integers 23 * @param x1, an integer 24 * @param y1, an integer 25 * @param x2, an integer 26 * @param y2, an integer 27 * @param target, an integer 28 * @return a boolean, indicate whether matrix contains target 29 */ 30 bool helper(vector<vector<int> > &M, int x1, int y1, int x2, int y2, int target) { 31 if (x1 > x2 || y1 > y2) {//empty matrix 32 return false; 33 } 34 int midx = (x1 + x2)>>1; 35 int indy = binarySearch(M[midx], y1, y2, target); 36 //M[midx][indy] <= target 37 if ((indy >= y1) && (M[midx][indy] == target)) { 38 return true; 39 } 40 return (helper(M, x1, indy+1, midx-1, y2, target))||(helper(M, midx+1, y1, x2, indy, target)); 41 42 } 43 /** 44 * @param matrix, a list of lists of integers 45 * @param target, an integer 46 * @return a boolean, indicate whether matrix contains target 47 */ 48 bool searchMatrix(vector<vector<int> > &matrix, int target) { 49 // write your code here 50 int m = matrix.size(); 51 if (m == 0) { 52 return false; 53 } 54 int n = matrix[0].size(); 55 return helper(matrix, 0, 0, m - 1, n - 1, target); 56 } 57 };?
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的LintCode: Search A 2d Matrix的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《c语言从入门到精通》看书笔记——第13
- 下一篇: 《c语言从入门到精通》看书笔记——第14