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 ...
随机推荐
- DB2 Vs MySQL系列 | MySQL与DB2的数据类型对比
随着MySQL数据库的应用越来越广泛,DB2向MySQL数据库的迁移需求也越来越多.进行数据库之间迁移的时候,首先遇到的并且也是最基本最重要的就是两种数据库数据类型之间的转换. 相关阅读: 从商用到开 ...
- C#基础-如何找到devenv的路径位置
一.前言 今年开始安装了VS2017,有时候需要使用到脚本编译,奈何MS在VS2017上的脚本编译上不再支持VS2015那种 "%VS140COMNTOOLS%vsvars32 ...
- 【uoj131】 NOI2015—品酒大会
http://uoj.ac/problem/131 (题目链接) 题意 给出一个字符串,每个后缀有一个权值${a_i}$,这些后缀两两之间存在公共前缀.问能够组成长度从0~n-1的公共前缀的后缀的方案 ...
- Python 使用CPickle和pickle模块进行序列化和反序列化
#Cpickle使用C语言进行编写的相比pickle来说效率高很多 #-*-coding:utf-8-*-'''序列化操作'''try: import cPickle as pickleexce ...
- spring管理hibernate,mybatis,一级缓存失效原因
mybatis缓存:一级缓存和二级缓存 hibernate缓存:一级缓存和二级缓存 关于缓存: 缓存是介于物理数据源与应用程序之间,是对数据库中的数据复制一份临时放在内存中的容器, 其作用是为了减少应 ...
- 【纪中集训2019.3.13】fft
题意: 描述 一共有\(n+m\)道题,其中\(n\)道答案是\(A\),\(m\)道答案是\(B\): 你事先知道\(n和m\),问在最优情况下的期望答错次数,对\(998244353\)取模: 范 ...
- List of NP-complete problems
This is a list of some of the more commonly known problems that are NP-complete when expressed as de ...
- Docker login报错一例
在一台ubuntu 18.04上执行docker login 登录镜像仓库的时候,抛出如下异常: error getting credentials - err: exit status 1, out ...
- 数据库sharding系列好文收藏
部分摘自于:http://my.oschina.net/u/188625/blog/104743 1. 又拍网架构中的数据库分库设计 . http://blog.csdn.net/nanjingjia ...
- 大量DOM操作的解决方案
案例:如何在页面元素ul中一次性插入30000个li标签,保证页面体验流畅呢? 解决方案:可以从减少 DOM 操作次数.缩短循环时间两个方面减少主线程阻塞的时间 减少 DOM 操作次数的良方是 Doc ...