Written exercises

table of Contents

1, completed find function --- to find whether a number in a two-dimensional array (vector objects), the difficulty is I do not know how to get the number of rows and columns in a two-dimensional array

2, supplement: C ++ in vector <vector <int >> A use of about  ****

1, completed find function --- to find whether a number in a two-dimensional array (vector objects), the difficulty is I do not know how to get the number of rows and columns in a two-dimensional array

. 1  class Solution {
 2  public :
 . 3      BOOL the Find ( int target, Vector <Vector < int >> Array) {
 . 4          // with rowCount, colCount array representing the number of rows and columns 
. 5          int the rowCount = array.size ();
 . 6          int colCount = array [ 0 ] .size ();
 . 7          / * is determined whether the array smaller than the minimum value or greater than the maximum value, so that an array can greatly improve efficiency.
8            array of length 0 is valid, so in order to avoid out of range determination 0 plus the length of the column * / 
. 9          IF (colCount == 0 || target <Array [ 0 ] [ 0] || target> Array [rowCount- . 1 ] [colCount- . 1 ]) {
 10              return  to false ;
 . 11          }
 12 is          // When the row is equal to 0 and the large length of the column is less than col colCount, to find whether there is target array 
13 is          for ( int = rowCount- Row . 1 , COL = 0 ; Row> = 0 && COL < colCount;) {
 14              IF (target == Array [Row] [COL]) {
 15                  return  to true ;
 16              } the else  IF (target < Array [Row ] [COL]) {
 . 17                  Row -;
18             }else if(target > array[row][col]){
19                 col++;
20             }
21         }
22         return false;
23     }
24 };
View Code

Method 2 (traversing method):

1 link: HTTPS: // www.nowcoder.com/questionTerminal/abc3fe2ce8e146608e868a70efebf62e?answerType=1&f=discussion 
2  Source: cattle off net
 3  
4  public  class Solution {
 5      public boolean the Find ( int target, int [] [] Array) {
 6          // by rowCount, colCount array representing the number of rows and columns 
. 7          int the rowCount = be array.length;
 . 8          int colCount = array [ 0 ] .length;
 . 9          / * determines the minimum value is smaller than the array, or more than the maximum the large value, so that when the array can greatly improve efficiency.
10            array of length 0 is valid, so in order to avoid cross-border columns plus 0 length determination * /
. 11          IF (colCount == 0 || target <Array [ 0 ] [ 0 ] || target> Array [rowCount- . 1 ] [colCount- . 1 ]) {
 12 is              return  to false ;
 13 is          }
 14          // When the row is equal to 0, and a large when less than the length of the column col colCount, find whether there is target array 
15          for ( int row = rowCount- . 1 , col = 0 ; row> = 0 && col < colCount;) {
 16              iF (target == array [row] [col ]) {
 . 17                  return  to true ;
 18 is              }else if(target < array[row][col]){
19                 row--;
20             }else if(target > array[row][col]){
21                 col++;
22             }
23         }
24         return false;
25     }
26 }
View Code

2, supplement: C ++ in vector <vector <int >> A use about

The following code contains the number of rows A, how to insert the data and how to obtain the number of columns of the A matrix and

. 1 #include <the iostream>
 2 #include <Vector>
 . 3  
. 4  the using STD :: Vector;
 . 5  the using STD COUT ::;
 . 6  the using STD :: endl;
 . 7  
. 8  int main ()
 . 9  {
 10      Vector <Vector < int >> a;
 . 11      // to define a = [[0,1,2], [ 3,4]], there are two methods
 12      @ method a: vector B are defined [0,1,2] and [ 3,4], and then put into vector A.
13 is      // Vector <int> B;
 14      // B.push_back (0); 
 15      // B.push_back (. 1);
 16      //B.push_back (2);
 . 17      // A.push_back (B);   // The B into A in
 18 is  
. 19      // B.clear ();   // Clear the element B
 20 is      // B.push_back (. 3 );
 21 is      // B.push_back (. 4);
 22 is      // A.push_back (B);   // the B into A in
 23 is  
24      // second method 
25      for ( int I = 0 ; I < 2 ; ++ I) A.push_back (vector < int > ());   // corresponds tells the compiler that a space is two rows (or two assigned vector objects) 
26 is      a [ 0 ] .push_back ( 0);
27     A[0].push_back(1);
28     A[0].push_back(2);
29     A[1].push_back(3);
30     A[1].push_back(4);
31 
32     int rowCount = A.size();  //获取A的行数
33     for (int i = 0; i < rowCount; i++)
34     {
35         for (int j = 0; j < A[i].size(); j++)  // A [i] .size () is the number of columns in the i-th row 
36              COUT << A [i] [J] << " \ T " ;
 37 [          COUT << endl;
 38 is      }
 39  
40  
41 is      System ( " PAUSE " );
 42 is      return  0 ;
 43 is }
View Code

Results of the:

 

Guess you like

Origin www.cnblogs.com/YiYA-blog/p/11516669.html