[Array]1. Two Sum(map和unorder_map)
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
思路:对于这种的不能进行排序,排序之后的元素下标就变的不一样了。 自己的代码:还是很低级,之前做过167了。。。。遍历实在浪费时间
vector<int> twoSum(vector<int>& nums, int target) {
vector<int>index;
int n = nums.size();
for(int i = ; i < n-; i++){
for(int j = i+; j < n; j++){
if(nums[i] + nums[j] == target){
index.push_back(i);
index.push_back(j);
//return vector<int>{i,j};
}
}
}
return index;
}
优秀代码:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int>temp;
for(int i = ; i < nums.size(); i++){
int sub = target = nums[i];
if(temp.find(sub) != nums.end()){
return vector<int>{temp[sub], i};
else
temp[nums[i]] = i; //这里将元素值和元素下标对应
}
}
}
unordered_map用法
[查找元素是否存在]
find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。
若有unordered_map<int, int> mp;查找x是否在map中
方法1: 若存在 mp.find(x)!=mp.end()
方法2: 若存在 mp.count(x)!=0
[插入数据]
map.insert(Map::value_type(1,"Raoul"));
[遍历map]
unordered_map<key,T>::iterator it;
(*it).first; //the key value
(*it).second //the mapped value
for(unordered_map<key,T>::iterator iter=mp.begin();iter!=mp.end();iter++)
cout<<"key value is"<<iter->first<<" the mapped value is "<< iter->second;
map用法
map经常用于c++中的二维数组,虽然我以前一直用vector<int, vector<int> >,还是要学着用一下map
参考:http://blog.csdn.net/u012530451/article/details/53228098; http://blog.csdn.net/bzhxuexi/article/details/24182087
[Array]1. Two Sum(map和unorder_map)的更多相关文章
- Golang高效实践之array、slice、map
前言 Golang的slice类型为连续同类型数据提供了一个方便并且高效的实现方式.slice的实现是基于array,slice和map一样是类似于指针语义,传递slice和map并不涉及底层数据结构 ...
- A. Array with Odd Sum Round #617(水题)
A. Array with Odd Sum time limit per test 1 second memory limit per test 256 megabytes input standar ...
- 【转载】Java集合类Array、List、Map区别和联系
Java集合类主要分为以下三类: 第一类:Array.Arrays第二类:Collection :List.Set第三类:Map :HashMap.HashTable 一.Array , Arrays ...
- LeetCode 548. Split Array with Equal Sum (分割数组使得子数组的和都相同)$
Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies fol ...
- java 集合类Array、List、Map区别和优缺点
Java集合类主要分为以下三类: 第一类:Array.Arrays 第二类:Collection :List.Set第三类:Map :HashMap.HashTable 一.Array , Array ...
- [LeetCode] Split Array with Equal Sum 分割数组成和相同的子数组
Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies fol ...
- Go语言学习笔记(四) [array、slice、map]
日期:2014年7月22日 一.array[数组] 1.定义:array 由 [n]<type> 定义,n 标示 array 的长度,而 <type> 标示希望存储的内 ...
- 表示集合的数据结构:数组(Array),对象(Object),Map和Set
Map和Set是ES6标准新增的数据类型 Map: 是一组键值对的结构,使用一个二维数组来初始化Map,例如: var m = new Map([['xiaohong',100],['xiaolan' ...
- JS内置对象-Array之forEach()、map()、every()、some()、filter()的用法
简述forEach().map().every().some()和filter()的用法 在文章开头,先问大家一个问题: 在Javascript中,如何处理数组中的每一项数据? 有人可能会说,这还不简 ...
随机推荐
- C++头文件记得加#pragma once
C++头文件记得加#pragma once不然可能会导致重定义类
- iOS开发系列-NSOutputStream
NSOutputStream 创建一个NSOutputStream实例 - (nullable instancetype)initToFileAtPath:(NSString *)path appen ...
- SpringCloud学习笔记《---04 Hystrix---》基础篇
- bzoj1010: [HNOI2008]玩具装箱toy——斜率优化
方程 $\Large f(i)=min(f(j)+(s(i)-s(j)-1-L)^2)$ 其中$s(i)$为i的前缀和再加上$i$ 对于某个$i$若$j$比$k$优,则 $\large f(j)+(s ...
- Jpgraph小应用
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 二.ES6新的声明方式
前言: 以前我们在声明时只有一种方法,就是使用var来进行声明,ES6对声明的进行了扩展,现在可以有三种声明方式了. 字面理解ES6的三种声明方式: var:它是variable的简写,可以理解成变量 ...
- leetcode-142-环形链表②
题目描述: 方法一:O(n) O(n) # Definition for singly-linked list. # class ListNode(object): # def __init__(se ...
- 工作中遇到的bug
1. Error: No PostCSS Config found in.. 在项目根目录新建postcss.config.js文件,并对postcss进行配置: module.exports = { ...
- 「题解」:[POJ2942]Knights of the Round Table
问题 E: Knights of the Round Table 时间限制: 1 Sec 内存限制: 256 MB 题面 题目描述 作为一名骑士是一个非常有吸引力的职业:寻找圣杯,拯救遇难的少女,与 ...
- java.util.concurrent中的几种同步工具类
java.util.concurrent并发包中提供了一系列的的同步工具类,这些基础类不管是否能在项目中使用到,了解一下使用方法和原理对java程序员来说都是有必要的.博主在看<java并发编程 ...