Offer to prove safety summary - Find two-dimensional array

class Solution {
public:
    bool Find(int target, vector<vector<int> > array) {
        if (array.size() == 0) {
            return false;
        }
        int rows = array.size();
        int cols = array[0].size();
        int j = cols - 1;
        int i = 0;
        while(i < rows && j >= 0) {
            if(array[i][j] < target) {
                ++i;
            }
            else if(array[i][j] > target) {
                --j;
            }
            else{
                return true;
            }
        }
        return false;
    }
};

This idea is very simple question, we look at the topic:

Focus is on each row from left to right in order of ascending sort each column are sorted in ascending order from top to bottom . Our ideas can be begins:

  1. , Is traversed from the first row to the last row to row from the end of the 0
  2. Each time come up with a number to compare and objectives, and if found returntrue
  3. After traversing to the end, the goal has not been found, it returns false

This is possible, but when the two-dimensional array is very large spend a lot of time will (probably in time complexity n^2, provided that two-dimensional array squarish and goals than by post), may not be able to meet the requirements of the subject, so we have to be optimized in accordance with the above focus on painting:

  1. First of all, it is certain that the end of the line, that is the rightmost digit is the greatest figures in this line, so if we find that our goal than this big you can jump directly to the next line for comparison
  2. If the rightmost digit is larger than the target, it can be determined only target we are looking for is on the left or below, we first consider the move to the left to find the target number of cases
  3. When we move to the left when there are only two cases, the current number is still larger than the target number, as well as smaller numbers than the target, if it is larger than the target number, we continue to go to the left, back to the situation will become final the latter case, that is small than the target number (if large numbers than the target, it can be determined to have been no comparable figures, because if go down, you can find the following numbers than the current large, because "each column are sorted in order of increasing top-down "and if going up, because we are on line before the adoption and maximum numbers than determined than digital numeric goals on the line, the target figure will certainly not on the line, therefore directly off the end of the cycle, return false)
  4. Because the digital time alignment on smaller than the target number, so we go straight down, the end does not need to return to the next line, because we can determine the next line numbers are larger than the numbers on one line in the same column, so you can determine this when the number to the right than the current number to be large (because the target figure is smaller than the number to the right of the line, and the number of the current line to the right of the big numbers off than on the same column of the row), and then we return to the situation 2 a similar procedure, it is found that large than the target left movement, the downward movement smaller than the target ......

To sum up, we need to do is:

  1. From the top row, beginning at the far right comparison, large moved leftward, the downward movement of the small
  2. When traversing the most to the left than the left than the bottom row to the next (i.e., beyond the boundary) when it is cut off cycle, returnsfalse

Guess you like

Origin www.cnblogs.com/yejianying/p/jianzhioffer_2d_array_searching.html