LeetCode - 1. Two Sum

简介: 1. Two Sum  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个数组nums和一个数target,求:id1和id2.

1. Two Sum 

Problem's Link

 ----------------------------------------------------------------------------

Mean: 

给定一个数组nums和一个数target,求:id1和id2. (nums[id1]+nums[id2]=target,id1和id2为数组nums两个不同的下标).

注意:nums中元素可重.

analyse:

如果nums中没有重复元素,那么可以用map做.

由于有重复元素,需要用multimap.

注意:使用map时,需要用count(key)来检测是否存在key值,否则会出现错误.

Time complexity: O(N*logN)

 

view code

/**
* -----------------------------------------------------------------
* Copyright (c) 2016 crazyacking.All rights reserved.
* -----------------------------------------------------------------
*       Author: crazyacking
*       Date  : 2015-01-29-14.24
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long( LL);
typedef unsigned long long( ULL);
const double eps( 1e-8);

class Solution {
public :
    vector < int > twoSum( vector < int >& nums , int target)
    {
            int cnt = 0;
            vector < int > ans;
            multimap < int , int > mp;
            for( auto p: nums)
                  mp . insert( make_pair(p , cnt ++));
            for( auto p: mp)
            {
                  if( mp . count( target -p . first) > 0)
                  {
                        multimap < int , int >:: iterator it1 , it2;
                        if((p . first == target -p . first) &&( mp . count(p . first) == 2))
                        {
                              it1 = mp . find(p . first);
                              ans . push_back(( * it1 ). second + 1);
                              mp . erase( it1);
                              it2 = mp . find(p . first);
                              ans . push_back(( * it2 ). second + 1);
                              break;
                        }
                        it1 = mp . find(p . first);
                        it2 = mp . find( target -p . first);
                        ans . push_back(( * it1 ). second + 1);
                        ans . push_back(( * it2 ). second + 1);
                        break;
                  }
            }
            sort( ans . begin (), ans . end());
            return ans;
    }
};

int main()
{
      int n;
      while( cin >>n)
      {
            vector < int > ve;
            for( int i = 0 , tmp; i <n; ++ i)
            {
                  cin >> tmp;
                  ve . push_back( tmp);
            }
            int target;
            cin >> target;
            Solution a;
            vector < int > ans = a . twoSum( ve , target);
            for( auto p : ans)
                  cout <<p << endl;
      }
      return 0;
}

 

目录
相关文章
|
11月前
|
存储 算法 安全
LeetCode - #1 Two Sum
我们社区从本期开始会将顾毅(Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。)的 Swift 算法题题解整理为文字版以方便大家学习与阅读。 不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。
LeetCode - #1 Two Sum
|
12月前
|
机器学习/深度学习
leetcode - two-sum
leetcode - two-sum
65 0
|
机器学习/深度学习 Android开发 C++
LeetCode之Two Sum
LeetCode之Two Sum
100 0
|
算法 索引 C++
[LeetCode]--15. 3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not
1235 0
[LeetCode]--112. Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tr
1218 0
[LeetCode]--1. Two Sum
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 n
1105 0