sweet1cris

Untitled

Jan 9th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.64 KB | None | 0 0
  1. // Binary Search Twice
  2. public class Solution {
  3.     public boolean searchMatrix(int[][] matrix, int target) {
  4.         if (matrix == null || matrix.length == 0) {
  5.             return false;
  6.         }
  7.         if (matrix[0] == null || matrix[0].length == 0) {
  8.             return false;
  9.         }
  10.        
  11.         int row = matrix.length;
  12.         int column = matrix[0].length;
  13.        
  14.         // find the row index, the last number <= target
  15.         int start = 0, end = row - 1;
  16.         while (start + 1 < end) {
  17.             int mid = start + (end - start) / 2;
  18.             if (matrix[mid][0] == target) {
  19.                 return true;
  20.             } else if (matrix[mid][0] < target) {
  21.                 start = mid;
  22.             } else {
  23.                 end = mid;
  24.             }
  25.         }
  26.         if (matrix[end][0] <= target) {
  27.             row = end;
  28.         } else if (matrix[start][0] <= target) {
  29.             row = start;
  30.         } else {
  31.             return false;
  32.         }
  33.        
  34.         // find the column index, the number equal to target
  35.         start = 0;
  36.         end = column - 1;
  37.         while (start + 1 < end) {
  38.             int mid = start + (end - start) / 2;
  39.             if (matrix[row][mid] == target) {
  40.                 return true;
  41.             } else if (matrix[row][mid] < target) {
  42.                 start = mid;
  43.             } else {
  44.                 end = mid;
  45.             }
  46.         }
  47.         if (matrix[row][start] == target) {
  48.             return true;
  49.         } else if (matrix[row][end] == target) {
  50.             return true;
  51.         }
  52.         return false;
  53.     }
  54. }
  55.  
  56. // Binary Search Once
  57. public class Solution {
  58.     /**
  59.      * @param matrix, a list of lists of integers
  60.      * @param target, an integer
  61.      * @return a boolean, indicate whether matrix contains target
  62.      */
  63.     public boolean searchMatrix(int[][] matrix, int target) {
  64.         // write your code here
  65.         if(matrix == null || matrix.length == 0){
  66.             return false;
  67.         }
  68.        
  69.         if(matrix[0] == null || matrix[0].length == 0){
  70.             return false;
  71.         }
  72.        
  73.         int row = matrix.length;
  74.         int column = matrix[0].length;
  75.        
  76.         int start = 0, end = row * column - 1;
  77.         while(start <= end){
  78.             int mid = start + (end - start) / 2;
  79.             int number = matrix[mid / column][mid % column];
  80.             if(number == target){
  81.                 return true;
  82.             }else if(number > target){
  83.                 end = mid - 1;
  84.             }else{
  85.                 start = mid + 1;
  86.             }
  87.         }
  88.        
  89.         return false;
  90.        
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment