My accepted Java solution


  • 0
    J
    public boolean searchMatrix(int[][] matrix, int target) {
    		boolean found = false;
    		int rows = matrix.length;
    		int columns = matrix[0].length;
    		int curRow = 0;
    		int curCol = columns - 1;
    
    		while (curRow < rows && curCol >= 0) {
              if(matrix[curRow][curCol]==target)//curRow*columns+curCol
              {
            	  found =true;
            	  break;
              }
              else if(matrix[curRow][curCol]<target){
            	  curRow++;
              }
              else {
            	  curCol--;
              }
    		}
          return found;
    	}

  • -1
    L

    from a book ? but you forget Null pointer judgment .


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.