2Sum

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.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

UPDATE (2016/2/13):
The return format had been changed to zero-based indices. Please read the above updated description carefully.

class Solution {
public:
  vector<int> twoSum(vector<int>& nums,int target){
        vector<int> res;
        unordered_map<int,int> mapping;
        for(size_t i=0;i<nums.size();i++){
            mapping[nums[i]] = i;
        }
        for(size_t i=0;i<nums.size();i++){
            const int key = target - nums[i];
            if(mapping.find(key) != mapping.end()){
                if(i != mapping[key]){
                    res.push_back(i);
                    res.push_back(mapping[key]);
                    break;
                }
            }
        }
        return res;
    }
};
时间: 2024-10-24 20:10:33

2Sum的相关文章

第一篇,2sum

1 public class Solution { 2 public int[] twoSum(int[] nums, int target) { 3 int[] result = {-1, -1}; 4 if (nums == null || nums.length < 2) { 5 return result; 6 } 7 HashMap<Integer, Integer> map = new HashMap(); 8 for (int i = 0; i < nums.leng

2Sum,3Sum,4Sum,kSum,3Sum Closest系列

1).2sum 1.题意:找出数组中和为target的所有数对 2.思路:排序数组,然后用两个指针i.j,一前一后,计算两个指针所指内容的和与target的关系,如果小于target,i右移,如果大于,j左移,否则为其中一个解 3.时间复杂度:O(nlgn)+O(n) 4.空间:O(1) 5.代码: void twoSum(vector<int>& nums,int numsSize,int target,vector<vector<int>>& two

一个&quot;2-SUM&quot;问题

题目要求: Download the text file here. (Right click and save link as). The goal of this problem is to implement a variant of the 2-SUM algorithm (covered in the Week 6 lecture on hash table applications). The file contains 1 million integers, both positi

[LeetCode] K sum(2Sum、3Sum、4Sum)

1 2Sum 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 not

2sum、3sum、4sum以及任意连续的数的和为sum、任意连续或者不连续的数的和为sum

2sum 如果数组是无序的,先排序(n*logn),然后用两个指针i,j,各自指向数组的首尾两端,令i=0,j=n-1,然后i++,j--,逐次判断a[i]+a[j]?=sum,如果某一刻a[i]+a[j]>sum,则要想办法让sum 的值减小,所以此刻i 不动,j--,如果某一刻a[i]+a[j]<sum,则要想办法让sum 的值增大,所以此刻i++,j 不动.所以,数组无序的时候,时间复杂度最终为O(n*logn+n)=O(n*logn),若原数组是有序的,则不需要事先的排序,直接O(n)

LeetCode 3Sum Closest 最近似的3sum(2sum方法)

题意:找到最接近target的3个元素之和,并返回该和. 思路:用2个指针,时间复杂度O(n^2). 1 int threeSumClosest(vector<int>& nums, int target) { 2 int sum=nums[0]+nums[1]+nums[2]; 3 sort(nums.begin(), nums.end()); 4 for(int i=0; i<nums.size()-2; i++) 5 { 6 int p=i+1, q=nums.size()

2Sum II - Input array is sorted

Given an array of integers that is already sorted in ascending order, 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 m

leetcode笔记:2Sum

一.题目描述 基本意思是给定一组整数和一个常数target,试图在这一组数里找到两个数使得两者的和等于target,结果要求返回两个数的下标. 二.解题思路 思路一:使用暴力算法实现,这种情况下空间复杂度为O(1),但是时间复杂度为O(n^2),会超时. 思路二:使用hash表,存储每个数对应的下标,事件复杂度为O(n).这样在查找某个值存不存在只需要常数时间. //LeetCode, Two Sum // 时间复杂度O(n),空间复杂度O(n) class Solution { public:

leetcode: 2Sum/3Sum/3SumClosest/4Sum系列问题(转载)

转载:http://blog.csdn.net/li4951/article/details/8693212 leetcode上有好几道关于数组中几个数据和为target的题目.恰好正在看剑指offer中"和为s的两个数组这章",据此思想,leetcode上的三道题目都被我解决了.总结一下. 1.twoSum: 输入一个递增数组和一个数字s,在数组中查找两个数使得它们的和正好是s. 既然题目中已经提到了"递增数组",那么肯定不会暴力了.因此肯定有<O(n*n)