【Leetcode】Two Sum

原创 2013年12月05日 21:58:46

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:
    vector<int> twoSum(vector<int> &numbers, int target) {
        vector<int> result;
        if(numbers.empty())
            return result;
        vector<int> backup(numbers);
        sort(numbers.begin(),numbers.end());
        getIndex(result,numbers,target);
        int index1,index2;
        bool finding=true;
        for(int i=0;i<backup.size();i++)
        {
            if(backup[i]==result[0]&&finding)
            {
                index1=i+1;
                finding=false;
                continue;
            }
            if(backup[i]==result[1])
                index2=i+1;
        }
        result.clear();
        result.push_back(index1);
        result.push_back(index2);
        sort(result.begin(),result.end());
        return result;
    }
    
    void getIndex(vector<int> &result,vector<int> &numbers,int target)
    {
        for(int i=numbers.size()-1;i>=0;i--)
        {
            if(target>numbers[i])
            {
                int index2=numbers[i];
                bool index1=find(numbers,target-numbers[i],0,i-1);
                if(index1)
                {
                    result.push_back(target-index2);
                    result.push_back(index2);
                    return;
                }
                else
                    continue;
            }
        }
    }
    
    bool find(vector<int> &numbers,int target,int start,int end)
    {
        if(end<start)
            return false;
        int medium=(start+end)>>1;
        if(target==numbers[medium])
            return true;
        else if(target<numbers[medium])
            return find(numbers,target,start,medium-1);
        else
            return find(numbers,target,medium+1,end);
    }
    
};

相关文章推荐

Two Sum leetcode c++

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

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

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

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

leetcode

【Leetcode】1. Two Sum

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

leetcode No.1 Two Sum

题意: 给定一个数组, 给定一个目标数, 找出数组中和等于目标数的两个数的位置。 题目相对简单。但是第一次接触leetcode觉得挺新鲜,选择C++语言答题后编辑框中是这样的: class Sol...

DAY1:leetcode #1 Two Sum

1、Two Sum Given an array of integers, return indices of the two numbers such that they add up to a...

Leetcode-1 Two Sum(Java) -by zzy

Problems: Given an array of integers, find two numbers such that they add up to a specific target n...

[LeetCode] 2011-03-13 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(Java实现)

leetcode测试地址:https://leetcode.com/problems/two-sum/#/description 1. Two Sum Given an array o...

1-Two Sum @LeetCode

1-Two Sum题目思路题目中得到的信息有: 都是整数,并且可正可负,也可一个值包含多个; 只有一个正确的结果。 方法一:最直接的思路就是两重循环遍历,时间复杂度是O(n^2),这样肯定不行。方法二...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【Leetcode】Two Sum
举报原因:
原因补充:

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