T1- two-dimensional array lookup

Title Description

(The length of each one-dimensional array of same), each row from left to right in order of ascending sort, to sort each column in a two-dimensional array in order of increasing from top to bottom. A complete function, enter such a two-dimensional array and an integer, it is determined whether the array contains the integer.

Time limit: C / C ++ 1 second, 2 seconds of space restrictions in other languages: C / C ++ 32M, 64M other languages heat index: 1,539,496
this question knowledge: find array

Problem-solving ideas

This question is to find a two-dimensional array, just to see this problem:

  1. The first reaction is raking array pattern: from left to right, top to bottom is incremented;
  2. Then nothing more than an array in accordance with the law, set to find a rule, the higher efficiency of the algorithm;
  3. Here there is a problem: when a two-dimensional array traversal, in accordance with custom, we usually from the array [0] [0] starting to traverse the ranks of the increasing order. But when we traverse the array in an element a [i] [j], if the target target> a [i] [j], the next is to find a [i + 1] [j], or a [i] [ j + 1]?
  4. (Of course, here the direct blunt whole array traversal again, is to get results, but it certainly is not the subject method desired)

The key point of the idea of ​​the problem lies in the need to findA regularSuch that: when the target target> a [i] [j] or target <a [i] [j], the traversal algorithm only one direction.

Here, I have an array of generally expressed it:
Here Insert Picture Description

Difficult to find any top to bottom and then from left to draw a fold line arrow (①, ②, ③) to right, the arrow through the array elements are satisfied in increasing order, and the array is given by the subject many so-called arrow constitution.

The inflection point of arrow a [i] [j] is compared with the target target:
if less than the target, then selecting a [i -1] [j] ;
if more than the target, then selecting a [i] [j + 1 ];
Thus, With the progressive process of the algorithm, naturally, initially, most elements of the array should be selected to participate in the lower left corner of the comparison.

The above-described process, as straightforward depiction of the algorithm is: initially, selecting the lower left corner of the array elements, when compared to the target target, if less than the target, then the element to the right of the selected element; if larger than the target, is selected the elements of the above elements. When out of bounds, the algorithm ends, the target is not in the target array.

Time complexity: O (n)
time complexity of the worst case: 2n

(Of course, the above-mentioned process, in turn, starting from the upper right corner of the array elements, are also possible)

Algorithm Description

class Solution {
public:
    bool Find(int target, vector<vector<int> > array) {
        if(array.size()!= 0)
        {
            int row = array.size()-1;
            int col = 0;//左下角元素坐标
            while(row >= 0 && col < array[0].size() )//数组不越界
            {
                if(array[row][col] == target)
                    return true;//找到
                else if(array[row][col] > target)
                    row--;//目标元素较小,往上找
                else
                    col++;//目标元素较大,往右找
            }           
        }
        return false;  
    }
};

supplement:

Finding common method

  1. Static lookup:
  • Find the order table: sequential search,
  • Find a sorted list: binary search, Fibonacci search, interpolation Find
  • Static lookup table tree:
  • Find the index order of the table:
  1. Dynamic look:
  • Binary sort trees and balanced binary tree Find
  • B + trees and B- trees to find
  • Trie lookup
  1. Hash lookup:

Common sequencing method

  1. Insertion sort:
  • Direct insertion sort
  • Other insertion sort: binary insertion, 2-way insertion, insertion table,
  • Hill sorting (narrow incremental sort)
  1. Quick Sort:
  • Bubble Sort
  1. Select Sort:
  • Simple selection sort
  • Tree Selection Sort
  • Heapsort
  1. Merge sort:
  2. Radix Sort:
  • Multi-keyword ranking
  • Chain radix sort
Published 24 original articles · won praise 0 · Views 2058

Guess you like

Origin blog.csdn.net/weixin_44849403/article/details/104034724