Initialization of two-dimensional vectors

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.