1#include <iostream>2#include <vector>3 using namespacestd;4 5 intMain ()6 {7 intm, N;8CIN >> M >>N;9vector<vector<int> > Value (M, vector<int> (n));//two > separated by a spaceTen for(inti =0; I < m; i++){ One for(intj =0; J < N; J + +){ A inttemp; -CIN >>temp; - Value[i].push_back (temp); the } - } - -}
Note the determination of the row and column size of two-dimensional vector
int row = value.size (); int col = value[0].size ();
With "Find in two-dimensional array" in "The Sword of Choice"
The title is described in a two-dimensional array in which each row is ordered in ascending order from left to right, and each column is sorted in ascending order from top to bottom. Complete a function, enter a two-dimensional array and an integer to determine if the array contains the integer. Give an example
1 2 8 9 2 4 9 A 4 7 Ten - 6 8 One the
To find out if 7 exists, compare the target number to the upper-right/lower-left corner of the array to narrow the search.
1 classSolution {2 Public:3 BOOLFind (vector<vector<int> > Array,inttarget) {4 if(Array.empty ())5 return false;6 intLenrow =array.size ();7 intLencol = array[0].size ();8 9 introw =0;Ten intCol = Lencol-1; One while(Row < Lenrow && Col >=0){ A if(Array[row][col] = =target) - return true; - Else if(Array[row][col] >target) thecol--; - Else -row++; - } + return false; - } +};
Initialization of two-dimensional vectors