码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]Two Sum

时间:2014-09-27 23:31:00      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:blog   io   os   ar   strong   for   sp   div   on   

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:利用map

// TwoSum2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <map>
using namespace std;
class Solution {
public:
	vector<int> twoSum(vector<int> &numbers, int target) {
		map<int, int>numMap;
		vector<int>res;
		map<int, int>::iterator map_lt;
		for (int i = 0; i < numbers.size(); i++)
		{
			numMap[numbers[i]] = i;
		}
		for (int i = 0; i < numbers.size(); i++)
		{
			map_lt = numMap.find(target - numbers[i]);
			if (map_lt != numMap.end() && numMap[target - numbers[i]] != i)
			{
				res.push_back(i+1);
				res.push_back(numMap[target - numbers[i]]+1);
				break;
			}
		}
		return res;
	}

};

int _tmain(int argc, _TCHAR* argv[])
{
	vector<int>nums;
	nums.push_back(2);
	nums.push_back(7);
	nums.push_back(11);
	nums.push_back(15);
	Solution ss;
	vector<int>res = ss.twoSum(nums,9);
	cout << res[0] << " " << res[1] << endl;
	system("pause");
	return 0;
}

  解法2:先排序,然后前后2个指针

// TwoSum.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <vector>
#include<iostream>
#include <algorithm>
using namespace std;
class Node{
public:
	int num;
	int pos;
};
bool cmp(Node a, Node b)
{
	return a.num < b.num;
}
class Solution {
public:
	vector<int> twoSum(vector<int> &numbers, int target) {
		vector<Node>arrays;
		vector<int>res;
		for (int i = 0; i < numbers.size(); i++)
		{
			Node tempNode;
			tempNode.num = numbers[i];
			tempNode.pos = i;
			arrays.push_back(tempNode);
		}
		sort(arrays.begin(),arrays.end(),cmp);
		int i, j;
		for (i = 0, j = arrays.size() - 1; i != j;)
		{
			if (arrays[i].num + arrays[j].num == target)
			{
				int min, max;
				max = arrays[i].pos >= arrays[j].pos ? arrays[i].pos : arrays[j].pos;
				min = arrays[i].pos <= arrays[j].pos ? arrays[i].pos : arrays[j].pos;
				res.push_back(min+1);
				res.push_back(max+1);
				break;
			}
			else if (arrays[i].num + arrays[j].num > target)
			{
				j--;
			}
			else if (arrays[i].num + arrays[j].num < target)
			{
				i++;
			}
		}
		return res;
	}
	
};
int _tmain(int argc, _TCHAR* argv[])
{	
	Solution ss;
	vector<int>nums;
	nums.push_back(2);
	nums.push_back(7);
	nums.push_back(11);
	nums.push_back(15);
	vector<int>res = ss.twoSum(nums, 9);
	cout << "index1=" << res[0] << ", index2=" << res[1] << endl;
	system("pause");
	
}

  

[LeetCode]Two Sum

标签:blog   io   os   ar   strong   for   sp   div   on   

原文地址:http://www.cnblogs.com/supernigel/p/3997344.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!