Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let searchMatrixRange = function(x1, x2, y1, y2, matrix, target) {
- console.log(x1, x2, y1, y2)
- if (x1 < 0 || x2 < 0 || x1 > x2 || x1 >= matrix[0].length || x2 >= matrix[0].length ||
- y1 < 0 || y2 < 0 || y1 > y2 || y1 >= matrix.length || y2 >= matrix.length) {
- console.log('return false');
- return false;
- }
- if (x1 === x2 && y1 === y2) {
- console.log('return matrix[x1][y1] === target:', matrix[x1][y1] === target)
- return matrix[x1][y1] === target;
- }
- let m = x2 - x1;
- let n = y2 - y1;
- let midM = Math.floor(m/2) + x1 ;
- let midN = Math.floor(n/2) + y1;
- let center = matrix[midM][midN];
- // console.log('center:',center);
- if (center === target) {
- return true;
- }
- if (center > target) {
- // console.log('center > target:', target);
- return (searchMatrixRange(x1, midM - 1, y1, midN, matrix, target) ||
- searchMatrixRange(midM, x2, y1, midN, matrix, target) ||
- searchMatrixRange(x1, midM, midN + 1, y2, matrix, target) )
- }
- if (center < target) {
- // console.log('center < target', target);
- // console.log((x1, x2, y1, y2);
- // console.log('midM:',midM);
- // console.log('midN:',midN);
- // console.log(midM + 1, x2, midN + 1, y2)
- return (searchMatrixRange(midM + 1, x2, midN + 1, y2, matrix, target) ||
- searchMatrixRange(midM, x2, y1, midN, matrix, target) ||
- searchMatrixRange(x1, midM, midN + 1, y2, matrix, target) )
- }
- };
- var searchMatrix = function(matrix, target) {
- return searchMatrixRange(0, matrix[0].length - 1, 0, matrix.length - 1, matrix, target);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement