Two Sum
来源:互联网 发布:家用菜刀 知乎 编辑:程序博客网 时间:2024/06/06 10:06
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.
class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { int index; vector<int> result; map<int, int> map; for( index = 0; index < nums.size(); index++ ) { if( !map.count( nums[index] ) ) map.insert(pair<int, int> (nums[index], index) ); if( map.count( target - nums[index] ) ) { int n = map[ target - nums[index] ]; if( n < index) { result.push_back( n ); result.push_back( index ); } } } return result; }};
0 0
- Two Sum
- Two Sum
- Two Sum
- two sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- 多态
- C++ 友元
- 面试算法-------确定两个字符串乱序同构
- 3.3Javascript函数-调用函数
- java中String\十六进制String\byte[]之间相互转换函数
- Two Sum
- Majority Element
- VS2010配置DirectX 11
- acm书中题目 p-16
- 从二维向量跟虚数的区别到欧拉公式,复指数函数
- 奋斗到永远!
- 如何搭建NDK环境并进行JNI开发?然后打包出动态so文件并在其他项目中使用?
- 数据结构之B进制(确定进制)
- Java 中静态变量和实例变量区别