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;
    }
};


相关文章推荐

Two Sum算法调试小demo

  • 2017年08月17日 18:01
  • 833B
  • 下载

Two Sum leetcode c++

  • 2015年10月01日 10:47
  • 763B
  • 下载

算法练习1.Two Sum 数组两个和为目标值的下标(map)

Two Sum 给定一个目标整数target,找出数组中和为target的两个数的下标 C++: class Solution { public:    vector twoSum(vector& n...

【LeetCode-面试算法经典-Java实现】【001-Two Sum(求两个数的和)】

给定一个整数数组,找出其中两个数满足相加等于你指定的目标数字。要求:这个函数twoSum必须要返回能够相加等于目标数字的两个数的索引,且index1必须要小于index2。请注意一点,你返回的结果(包...

无序数组中找出和为N的两个数 Two Sum

如果是有序数组,很简单。两头指针往中间相遇即可。 因此对于无序数组,排序即可。 唯一的麻烦题目要求的:要返回两个数在排序之前原数组里的序号。因此我需要在排序时也要保留原来的序列号。 这里主要是以前...

从今天起,刷题看数据结构算法——leetcode题 two sum

leetcode

#1 Two Sum

Given an array of integers, find two numbers such that they add up to a specific target number. The...

【Leetcode】1. Two Sum

1. Two Sum Total Accepted: 254411Total Submissions: 1031847Difficulty: Easy Given an array...

29. Divide Two Integers/49. Group Anagrams/96. Unique Binary Search Trees/560. Subarray Sum Equals K

Divide Two Integers Problem Description Implementation29. Divide Two IntegersProblem DescriptionDivi...

leetcode No.1 Two Sum

题意: 给定一个数组, 给定一个目标数, 找出数组中和等于目标数的两个数的位置。 题目相对简单。但是第一次接触leetcode觉得挺新鲜,选择C++语言答题后编辑框中是这样的: class Sol...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Two Sum
举报原因:
原因补充:

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