2016.07.13-vector<vector<int>> Apply 2--two sum extension

Source: Internet
Author: User

Harvest:

Vector<vector<int> >res, cannot be used directly with Res[j].push_back (number), because Res[j] is empty, not initialized

You can define vector<int> Innumer first, Res.push_back (Innumber).

Just find a set of matching outputs in sum, and I want to output all the matching combinations in the array.

1#include"stdafx.h"2#include"Vector"3#include"Map"4#include"iostream"5#include"unordered_map"        //Unordered_map's header file6 using namespacestd;7 8 classMyClass9 {Ten  Public: Onevector<vector<int> > Twosum (vector<int> &nums,inttarget) A     { -unordered_map<int,int> hash;//the hash table,<key,value> with the initial alias as hash is int type -         intSize =nums.size (); thevector<vector<int> > Res;//first define a vector<vector<int> > store all eligible combinations -vector<int> inIt;//store each combination of eligible -         intj =0; -          for(inti =0; i < size; i++) +         { -             intNumtofind = target-Nums[i]; +             if(Hash.find (numtofind)! =hash.end ()) A             { atInit.push_back (Hash[numtofind]);//put the number of each group into Init first - Init.push_back (i);  -Res.push_back (inIt);//put this group in res -Init.clear ();//clear values for each group -             } -Hash[nums[i]] = i;//Place the values in the vector in the map in         } -         returnRes; to     }  + }; -  the int_tmain (intARGC, _tchar*argv[]) * { $vector<int> nums = {1,2,3,4,4,9,8,Ten };Panax Notoginseng     inttarget =5; -vector<vector<int> >Res;  the MyClass solution; +res =solution.twosum (nums, target); A     intSize =res.size (); the      for(inti =0; i < size; i++) +     { -         intVsize =res[i].size (); $cout <<"["; $          for(intj =0; J < Vsize; J + +) -         { -cout << Res[i][j] <<" "; the         } -cout <<"]";Wuyi     } thecout <<Endl; -System"Pause"); Wu     return 0; -}

2016.07.13-vector<vector<int>> Apply 2--two sum extension

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.