Advertisement
Guest User

Untitled

a guest
Feb 17th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let searchMatrixRange = function(x1, x2, y1, y2, matrix, target) {
  2.     console.log(x1, x2, y1, y2)
  3.     if (x1 < 0 || x2 < 0 || x1 > x2 || x1 >= matrix[0].length || x2 >= matrix[0].length ||
  4.         y1 < 0 || y2 < 0 || y1 > y2 || y1 >= matrix.length || y2 >= matrix.length) {
  5.         console.log('return false');
  6.         return false;
  7.     }
  8.     if (x1 === x2 && y1 === y2) {
  9.         console.log('return matrix[x1][y1] === target:', matrix[x1][y1] === target)
  10.         return matrix[x1][y1] === target;
  11.     }
  12.     let m = x2 - x1;
  13.     let n = y2 - y1;
  14.     let midM = Math.floor(m/2) + x1 ;
  15.     let midN = Math.floor(n/2) + y1;
  16.    
  17.     let center = matrix[midM][midN];
  18.     // console.log('center:',center);
  19.     if (center === target) {
  20.         return true;
  21.     }
  22.     if (center > target) {
  23.         // console.log('center > target:', target);
  24.         return (searchMatrixRange(x1, midM - 1, y1, midN, matrix, target) ||
  25.                 searchMatrixRange(midM, x2, y1, midN, matrix, target) ||
  26.                 searchMatrixRange(x1, midM, midN + 1, y2, matrix, target) )
  27.     }
  28.     if (center < target) {
  29.         // console.log('center < target', target);
  30.         // console.log((x1, x2, y1, y2);
  31.         // console.log('midM:',midM);
  32.         // console.log('midN:',midN);
  33.         // console.log(midM + 1, x2, midN + 1, y2)
  34.         return (searchMatrixRange(midM + 1, x2, midN + 1, y2, matrix, target) ||
  35.                 searchMatrixRange(midM, x2, y1, midN, matrix, target) ||
  36.                 searchMatrixRange(x1, midM, midN + 1, y2, matrix, target) )
  37.     }
  38.    
  39.    
  40. };
  41. var searchMatrix = function(matrix, target) {
  42.     return searchMatrixRange(0, matrix[0].length - 1, 0, matrix.length - 1, matrix, target);
  43. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement