Topic three: two-dimensional array lookup

// find the problem described two-digit groups
// in a two-dimensional array (the same as the length of each one-dimensional array), each row from left to right in order of ascending sort, each column from top to bottom in accordance with increasing order.
// a complete function, enter such a two-dimensional array and an integer, it is determined whether the array contains the integer.

FindNum BOOL (int target, Vector <Vector <int>> vect)
{
     int = iRow vect.size ();
     int iCol = vect [0] .size ();
    
     // start traversing from the top left
     int i = 0, j iCol = -. 1;
     the while (I <= iRow -. 1 && J> = 0) // array can not be guaranteed bounds
     {
         IF (vect [I] [J]> target)
         {
             J,;
         }
         the else IF (vect [I ] [J] <target)
         {
             I ++;
         }
         the else
         {
             return to true;
         }
     }

    return false;
}

int DoubleArrayTest()
{
     //7, [[1, 2, 8, 9], [2, 4, 9, 12], [4, 7, 10, 13], [6, 8, 11, 15]]
     int aiData[4][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
     vector<vector<int>> res;

    cout << "初始化二位数组: " << endl;
     for (int i = 0; i < 4; i++)
     {
         vector<int> vect;
         for (int j = 0; j < 4; j++)
         {
             vect.push_back(aiData[i][j]);
         }
         res.push_back(vect);

        TraversalArray(res[i]);
     }

    if (FindNum(7, res))
     {
         cout << "true" << endl;
     }
     else
     {
         cout << "false" << endl;
     }

    return 0;
}

Guess you like

Origin www.cnblogs.com/yzdai/p/11258595.html