[LeetCode] Two Sum [17]

题目

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. 所给数组不是有序的;

2. 返回的下标是从1开始的,并且是原来无序数组中的下标;

3. 输入数组中可能含有重复的元素。

好了,把以上三点想到的话,做这个题应该不会有啥问题。

具体方法:把原数组拷贝一份,求出拷贝的数组中符合题意的两个数;再去原数组中找到前面两个数的位置,返回即可。

代码实现

class Solution {
public:
    vector<int> twoSum(vector<int> &numbers, int target) {
        vector<int> ret;
        int n = numbers.size();
        if(n<=0) return ret;
        int i=0, j=n-1;
        int sum;
        vector<int> copy(numbers);
        sort(copy.begin(), copy.end());
        while(i<=j){
            sum = copy[i]+copy[j];
            if(sum>target) --j;
            else if(sum<target) ++i;
            else break;
        }
        if(i<=j){
            for(int k=0; k<n; ++k){
                if(numbers[k] == copy[i]){
                    ret.push_back(k+1);
                }else if(numbers[k] == copy[j]){
                    ret.push_back(k+1);
                }
            }
        }
        return ret;
    }
};

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

如果你觉得本篇对你有收获,请帮顶。

另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.

你可以搜索公众号:swalge 或者扫描下方二维码关注我

(转载文章请注明出处: http://blog.csdn.net/swagle/article/details/29200949
)

[LeetCode] Two Sum [17]

时间: 2024-11-07 22:11:44

[LeetCode] Two Sum [17]的相关文章

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

Leetcode:Path Sum 二叉树路径和

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 tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return

Leetcode | Combination Sum I &amp;&amp; II

Combination Sum I Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note:All numbers (includ

[leetcode]Two Sum @ Python

原题地址:http://oj.leetcode.com/problems/two-sum/ 题意:找出数组numbers中的两个数,它们的和为给定的一个数target,并返回这两个数的索引,注意这里的索引不是数组下标,而是数组下标加1.比如numbers={2,7,11,17}; target=9.那么返回一个元组(1,2).这道题不需要去重,对于每一个target输入,只有一组解,索引要按照大小顺序排列. 解题思路:1,由于要找到符合题意的数组元素的下标,所以先要将原来的数组深拷贝一份,然后排

leetcode -- 3 sum

3-sum 题目描述: 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. 题目要求: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ 

[leetcode]Combination Sum @ Python

原题地址:https://oj.leetcode.com/problems/combination-sum/ 题意: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited

LeetCode: Combination Sum [038]

[题目] Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target)

LeetCode: Combination Sum II [039]

[题目] Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

LeetCode——Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 给定一个二叉树和一个值,找出所有根到叶的路径和等于