2016.07.13-vector<vector<int>>应用2——Two Sum扩展
收获:
vector<vector<int> >res,不能直接用res[j].push_back(number),因为res[j]是空的,没有初始化
可以先定义 vector<int> inNumer, res.push_back(inNumber)即可。
Two Sum中仅仅找出一组符合的输出即可,我希望将数组中所有符合的组合都输出。
#include "stdafx.h"
#include "vector"
#include "map"
#include "iostream"
#include "unordered_map" //unordered_map的头文件
using namespace std; class MyClass
{
public:
vector<vector<int> > twoSum(vector<int> &nums, int target)
{
unordered_map<int, int> hash; //初始化名为hash的hash table,<key,value>均为int型
int size = nums.size();
vector<vector<int> > res; //先定义一个vector<vector<int> > 存放所有符合条件的组合
vector<int> inIt; //存放每一个符合条件的组合
int j = ;
for (int i = ; i < size; i++)
{
int numToFind = target - nums[i];
if (hash.find(numToFind) != hash.end())
{
inIt.push_back(hash[numToFind]); //先将每组的数放入到inIt中
inIt.push_back(i);
res.push_back(inIt); //将这个组放入到res中
inIt.clear(); //清除每组的值
}
hash[nums[i]] = i; //将vector中的值放到map中
}
return res;
}
}; int _tmain(int argc, _TCHAR* argv[])
{
vector<int> nums = { , , , , , , , };
int target = ;
vector<vector<int> > res;
MyClass solution;
res = solution.twoSum(nums, target);
int size = res.size();
for (int i = ; i < size; i++)
{
int vsize = res[i].size();
cout << "[";
for (int j = ; j < vsize; j++)
{
cout << res[i][j] << " ";
}
cout << "]";
}
cout << endl;
system("pause");
return ;
}
2016.07.13-vector<vector<int>>应用2——Two Sum扩展的更多相关文章
- 2016.6.24——vector<vector<int>>【Binary Tree Level Order Traversal】
Binary Tree Level Order Traversal 本题收获: 1.vector<vector<int>>的用法 vector<vector<int ...
- vector< vector<int> >类似于二维数组
vector< vector<int> > intVV; vector<int> intV; int i,j; ;i<;++i){ intV.clear(); ...
- const vector<int> 和 vector<const int>问题讨论
1.const vector <int> vec(10) —— 与const int a[10]是一回事,意思是vec只有10个元素,不能增加了,里面的元素也是不能变化的 vector&l ...
- 对多维向量vector<vector<int> > vec进行操作
直接写作vector<vector<int> > vec在VC++6.0下编译不过改做: typedef std::vector<int> ROW; s ...
- vector vector int 初始化
方法一: vector<vector<int>>array=(2,vector<int>()); array[0].push_back(1); array[i].p ...
- vector<vector<int>> 使用简单示例
#include <iostream> #include <vector> using namespace std; int main() { vector<vector ...
- CodeForces - 93B(贪心+vector<pair<int,double> >+double 的精度操作
题目链接:http://codeforces.com/problemset/problem/93/B B. End of Exams time limit per test 1 second memo ...
- 关于C++中vector<vector<int> >的使用
1.定义 vector<vector<int>> A;//错误的定义方式 vector<vector<int> > A;//正确的定义方式 2.插入元素 ...
- NDK GDB 中打印vector , vector<vector <> >
在android上进行native开发的时候,我们需要用NDK-GDB 对native code进行调试,其中很麻烦的是,我使用的NDK版本是4.0,该版本还不支持用NDK-GDB直接打印vector ...
随机推荐
- Delphi 判断一个字符串是否为数字
//函 数 名: IsDigit//返 回 值: boolean//日 期:2011-03-01//参 数: String//功 能: 判断一个字符串是否为数字// ...
- Acdream1311_Apple
无聊的时候看到上一次acdream群赛的一个题目,中间居然是有alice和bob的博弈题目,于是就去做了. 给n,m,两人轮流操作,每次操作可以使n+1,或者m+1,谁操作后满足nm>=A,那么 ...
- python自动化之word文档
#########################docx文件############################ ''' .docx文件有很多结构,有3种不同的类型来表示 在最高一层,Docum ...
- jsp中的下载链接
1.下载链接jsp界面(a链接直接链文件可以看出文件在服务器中的路径,用servlet处理的链接则看不出) <%@ page language="java" contentT ...
- 关于AC自动机和DP的联系
首先是描述个大概.不说一些特殊的DP 或者借用矩阵来状态转移 (这些本质都是一样的). 只讲AC自动机和DP的关系(个人理解). AC自动机 又可以叫做状态机. 我一开始的认为.AC 自动机提供了一些 ...
- 51nod 1421 最大MOD值 | 暴力
题面 有一个a数组,里面有n个整数.现在要从中找到两个数字(可以是同一个) ai,aj ,使得 ai mod aj 最大并且 ai ≥ aj. Input 单组测试数据. 第一行包含一个整数n,表示数 ...
- 洛谷 P2184 贪婪大陆 解题报告
P2184 贪婪大陆 题目背景 面对蚂蚁们的疯狂进攻,小\(FF\)的\(Tower\) \(defence\)宣告失败--人类被蚂蚁们逼到了\(Greed\) \(Island\)上的一个海湾.现在 ...
- 洛谷 P1777 帮助_NOI导刊2010提高(03) 解题报告
P1777 帮助_NOI导刊2010提高(03) 题目描述 Bubu的书架乱成一团了!帮他一下吧! 他的书架上一共有n本书.我们定义混乱值是连续相同高度书本的段数.例如,如果书的高度是30,30,31 ...
- python定义函数以及参数检查
(转自廖雪峰网站) 函数定义 在Python中,定义一个函数要使用def语句,依次写出函数名.括号.括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回. 我们以自定义 ...
- my-innodb-heavy-4G.cnf配置文件注解
[client] ####客户端 port = 3306 ####mysql客户端连接时的默认端口号 socket = /application/mysql-5.5.32/tmp/mysql.sock ...