[LeetCode] Two Sum

简介: 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 targe

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

/*****************************************************************************
    *  @COPYRIGHT NOTICE
    *  @Copyright (c) 2015, 楚兴
    *  @All rights reserved
    *  @Version  : 1.0

    *  @Author   : 楚兴
    *  @Date     : 2015/2/5 13:40
    *  @Status   : Accepted
    *  @Runtime  : 22 ms
*****************************************************************************/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
	vector<int> twoSum(vector<int> &numbers, int target) {
		vector<int> indices;
		vector<int> temp(numbers);
		sort(numbers.begin(),numbers.end());
		for (int i = 0; i < numbers.size() - 1; i++)
		{
			for (int j = i + 1; j < numbers.size(); j++)
			{
				if (numbers[i] + numbers[j] == target)
				{
					int n1 = find(temp,numbers[i]);
					int n2 = find(temp,numbers[j], n1 - 1);
					if (n1 < n2)
					{
						indices.push_back(n1);
						indices.push_back(n2);
					}
					else
					{
						indices.push_back(n2);
						indices.push_back(n1);
					}

					return indices;
				}
				if (numbers[i] + numbers[j] > target)
				{
					break;
				}
			}
		}
	}

private:
	int find(vector<int> num, int n, int flag = -1)
	{
		for (int i = 0; i < num.size(); i++)
		{
			if (i == flag)
			{
				continue; //不能和前一个点序号相同
			}
			if (num[i] == n)
			{
				return i + 1;
			}
		}
	}
};

int main()
{
	vector<int> nums;
	nums.push_back(2);
	nums.push_back(7);
	nums.push_back(11);
	nums.push_back(14);
	Solution s;
	vector<int> indices = s.twoSum(nums,9);
	return 0;
}

// Runtime: 28 ms
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int, int> mymap;
        vector<int> indices;
        for (int i = 0; i < nums.size(); i++)
        {
            if (mymap.find(target - nums[i]) != mymap.end())
            {
                indices.push_back(mymap[target-nums[i]] + 1);
                indices.push_back(i + 1);
            }
            else
            {
                mymap[nums[i]] = i;
            }
        }

        return indices;
    }
};

目录
相关文章
|
存储 缓存 算法
LeetCode刷题---Two Sum(一)
LeetCode刷题---Two Sum(一)
|
11月前
Leetcode 4. Median of Two Sorted Arrays
题目描述很简单,就是找到两个有序数组合并后的中位数,要求时间复杂度O(log (m+n))。 如果不要去时间复杂度,很容易就想到了归并排序,归并排序的时间复杂度是O(m+n),空间复杂度也是O(m+n),不满足题目要求,其实我开始也不知道怎么做,后来看了别人的博客才知道有个二分法求两个有序数组中第k大数的方法。
32 0
|
存储 C++ Python
LeetCode刷题---Add Two Numbers(一)
LeetCode刷题---Add Two Numbers(一)
|
存储 算法 安全
LeetCode - #2 Add Two Numbers
我们社区从本期开始会将顾毅(Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。)的 Swift 算法题题解整理为文字版以方便大家学习与阅读。 不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。
LeetCode - #2 Add Two Numbers
|
存储 算法 安全
LeetCode - #1 Two Sum
我们社区从本期开始会将顾毅(Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。)的 Swift 算法题题解整理为文字版以方便大家学习与阅读。 不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。
LeetCode - #1 Two Sum
LeetCode 350. Intersection of Two Arrays II
给定两个数组,编写一个函数来计算它们的交集。
71 0
LeetCode 350. Intersection of Two Arrays II
|
Python
LeetCode 349. Intersection of Two Arrays
给定两个数组,编写一个函数来计算它们的交集。
64 0
LeetCode 349. Intersection of Two Arrays
LeetCode 160. Intersection of Two Linked Lists
编写一个程序,找到两个单链表相交的起始节点。
60 0
LeetCode 160. Intersection of Two Linked Lists
LeetCode 167 Two Sum II - Input array is sorted(输入已排序数组,求其中两个数的和等于给定的数)
给定一个有序数组和一个目标值 找出数组中两个成员,两者之和为目标值,并顺序输出
81 0
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists