Two Sum

原创 2015年07月07日 23:04:32

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


class Solution {
public:
    template <typename T>
    vector<size_t> sort_indexes(vector<T> &v) {

        // initialize original index locations
         vector<size_t> idx(v.size());
        for (size_t i = 0; i != idx.size(); ++i) idx[i] = i;

        // sort indexes based on comparing values in v
        sort(idx.begin(), idx.end(),
             [&v](size_t i1, size_t i2) {return v[i1] < v[i2];});
        sort(v.begin(), v.end());
        return idx;
    }
    int select(vector<int>& nums, int start, int end, int value)
    {
        if(start > end)
            return -1;
        if(nums[(start + end)/2] == value)
            return (start + end)/2;
        else if(nums[(start + end)/2] < value)
        {
            start = (start + end)/2 + 1;
            select(nums, start, end, value);
        }
        else{
            end = (start + end)/2 -1;
            select(nums, start, end, value);
        }
    }
    vector<int> twoSum(vector<int>& nums, int target) {
        //sorted nums ascendingly.
        vector<size_t> sorted_indexes = sort_indexes(nums);
        
        int result;
        vector<int> two_sum;
        for(int i = 0; i < nums.size(); i++)
        {
            //look for number = target-nums[i]
            int index = select(nums, i+1, nums.size() - 1, target-nums[i]);
            if(index != -1)
            {
                int a1 = 0;
                int a2 = 0;
                
                if(sorted_indexes[i] < sorted_indexes[index]){
                   a1 = sorted_indexes[i]+1;
                   a2 = sorted_indexes[index] + 1;
                }
                else{
                    a1 = sorted_indexes[index] + 1;
                    a2 = sorted_indexes[i]+1;
                }
                two_sum.push_back(a1);
                two_sum.push_back(a2);
                return two_sum;
            }
        }
        return two_sum;
    }
};


LeetCode::Two Sum C语言

题目 Given an array of integers, find two numbers such that they add up to a specific target number. ...
  • u013300875
  • u013300875
  • 2015年03月07日 15:39
  • 5513

leetcode 刷题题解(c++) 1.Two Sum (hash表,排序+二分查找)

c++刷leetcode,有题目和算法代码,可直接在leetcode提交通过。
  • GOGO_YAO
  • GOGO_YAO
  • 2016年07月24日 20:59
  • 648

算法系列--Two Sum

继续算法系列。今天再看一道leetcode(www.leetcode.com)的题目。问题描述原文 Given an array of integers, find two numbers suc...
  • ylyg050518
  • ylyg050518
  • 2015年09月13日 23:34
  • 928

Two Sum -- LeetCode

原题链接: http://oj.leetcode.com/problems/two-sum/ 这是一道非常经典的题目,brute force时间复杂度为O(n^2), 对每一对pair两两比较。 优...
  • linhuanmars
  • linhuanmars
  • 2014年02月22日 23:11
  • 20487

LeetCode 1 Two Sum 题解

Problem: Given an array of integers, find two numbers such that they add up to a specific target nu...
  • runningtortoises
  • runningtortoises
  • 2015年05月05日 16:53
  • 1249

[LeetCode]1 Two Sum(C++,Python实现)

LeetCode OJ的第一题,题目描述如下:
  • Shiroh_ms08
  • Shiroh_ms08
  • 2014年05月16日 21:17
  • 1864

leetcode 170: Two Sum III - Data structure design

Two Sum III - Data structure design Total Accepted: 311 Total Submissions: 1345 Design and implem...
  • xudli
  • xudli
  • 2014年12月31日 08:17
  • 6981

LeetCode || Two Sum

Two Sum  Total Accepted: 16363 Total Submissions: 87273My Submissions Given an array of ...
  • jiadebin890724
  • jiadebin890724
  • 2014年04月09日 23:11
  • 33862

leetcode 1. Two Sum -----java

题目链接:https://leetcode.com/problems/two-sum/ 题目: Given an array of integers, return indices of th...
  • j754379117
  • j754379117
  • 2016年06月20日 19:52
  • 1404

[LeetCode][1]Two Sum解析 -Java实现

Q: Given an array of integers, return indices of the two numbers such that they add up to a spe...
  • u014629433
  • u014629433
  • 2016年06月01日 16:23
  • 830
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Two Sum
举报原因:
原因补充:

(最多只允许输入30个字)