描述:

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

思路1:暴力搜索,两层循环。时间:O(n^2),空间:O(1),可能出现超时

思路2:考虑使用map或者hash_map,遍历输入的numbers,在map映射表中寻找target-numbers[i],C++ STL中map使用红黑树实现,查找元素时间O(logn),总时间O(nlogn)

 class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) { vector<int> result;
map<int, int> hashMap; for(int i = ; i < numbers.size(); i++) { if(!hashMap.count(numbers[i]))
hashMap.insert(pair<int,int>(numbers[i], i)); // <value, key> if(hashMap.count(target - numbers[i])) {
int n = hashMap[target - numbers[i]]; //get the second number's position
if(n < i) {
result.push_back(n + );
result.push_back(i + );
return result;
} //unnecessary
/*if(n > i) {
result.push_back(i + 1);
result.push_back(n + 1);
return result;
}*/
}
}
}
};

附LeetCode建议解决方案

LeetCode Problem 2:Two Sum的更多相关文章

  1. LeetCode之“树”:Path Sum && Path Sum II

    Path Sum 题目链接 题目要求: Given a binary tree and a sum, determine if the tree has a root-to-leaf path suc ...

  2. LeetCode第一题:Two Sum

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  3. [LeetCode]题1:two sum

      Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0 ...

  4. LeetCode 题解(一):Two Sum

    LeetCode : two sum 第一次写博客,算是熟悉这些编辑环境吧,本来是打算在csdn上用markdown写的,结果改了博客介绍就被关闭了,晕死...好了,话不多说,今天打算拿LeetCod ...

  5. 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)

    昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...

  6. [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  7. 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)

    转自  http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...

  8. [LeetCode] 327. Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  9. (Problem 13)Large sum

    Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. 371072875339 ...

随机推荐

  1. eclipse sun.net 下包无法导入问题

    项目中用到了:sun.net.ConnectionResetException.但是sun.net包里的类,在eclipse里默认是不让用的. 解决办法是自定义access rules 工程上右键-& ...

  2. Eclipse重新导入Tomcat启动出错选择不了文件夹

    如果你已经把tomcat配置进了Eclipse,却因为某些原因delete了服务器,再想导入同版本的服务器就有可能会报Could not publish to the server.错误,并且添加服务 ...

  3. docker实战——Docker本地私有镜像仓库Harbor搭建及配置

    Harbor介绍 Docker容器应用的开发和运行离不开可靠的镜像管理,虽然docker官方提供了公共的镜像仓库(Docker Hub),但是从安全和效率等方面考虑,部署我们私有环境内的Registr ...

  4. jumpserverv0.5.0 基于 CentOS7安装部署

    基于 CentOS 7 一步一步安装 Jumpserver 0.5.0 环境 系统: CentOS 7 IP: 192.168.244.144 关闭 selinux和防火墙 # CentOS 7 $ ...

  5. CentOS 加载/挂载光驱

    1.mkdir /mnt/cdrom 2.mount /dev/cdrom /mnt/cdrom 3.挂载      umount /dev/cdrom /mnt/cdrom

  6. Java实现XSS防御

    XSS概述 跨站脚本攻击(Cross Site Scripting),缩写为XSS.恶意攻击者往Web页面里插入恶意Script代码,当用户浏览该页之时,嵌入其中Web里面的Script代码会被执行, ...

  7. ORA-1092 : opitsk aborting process---killed by oom killer

    oracle version:Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production os versi ...

  8. Atitit  文件上传  架构设计 实现机制 解决方案  实践java php c#.net js javascript  c++ python

    Atitit  文件上传  架构设计 实现机制 解决方案  实践java php c#.net js javascript  c++ python 1. 上传的几点要求2 1.1. 本地预览2 1.2 ...

  9. 【LeetCode OJ 016】3Sum Closest

    题目链接:https://leetcode.com/problems/3sum-closest/ 题目:Given an array S of n integers, find three integ ...

  10. Unity3d Serialize问题

    备忘: 1. ScriptableOjbect中,由于Serialization的原因,不能使用基类引用来存储子类对象,这样都会导致数据丢失 2. 无法直接对Unity的数据如,vector3, qu ...