[LintCode] Two Sum 两数之和

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.

Notice

You may assume that each input would have exactly one solution

Have you met this question in a real interview?

Yes

Example

numbers=[2, 7, 11, 15], target=9

return [1, 2]

Challenge

Either of the following solutions are acceptable:

  • O(n) Space, O(nlogn) Time
  • O(n) Space, O(n) Time

LeetCode上的原题,请参见我之前的博客Two Sum

class Solution {
public:
    /*
     * @param numbers : An array of Integer
     * @param target : target = numbers[index1] + numbers[index2]
     * @return : [index1+1, index2+1] (index1 < index2)
     */
    vector<int> twoSum(vector<int> &nums, int target) {
        unordered_map<int, int> m;
        for (int i = 0; i < nums.size(); ++i) m[nums[i]] = i;
        for (int i = 0; i < nums.size(); ++i) {
            int t = target - nums[i];
            if (m.count(t) && m[t] != i) {
                return {i + 1, m[t] + 1};
            }
        }
        return {};
    }
};
时间: 2024-08-05 15:23:24

[LintCode] Two Sum 两数之和的相关文章

leetcode——Two Sum 两数之和(AC)

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

LeetCode Two Sum 两数之和

题意:在一个整数序列中找到两个元素,他们之和为target,并用vector返回这两个元素的位置(升序),位置从1开始算起. 思路: 方法(1):两个指针法.也就是排序,然后一个从头扫,一个从尾扫,前提是先排序,但是给的数组是无序的,一旦排序就失去了他们的具体位置.如果是ACM的题还可以弄个结构体把他们打包起来,再根据值大小来排序,这样找到了也就能找到他们的位置.可是这里是在一个类里面的,用不上结构体,肯定还有其他方法 ,比如 pair,但是我还不会实现.于是,用multimap来代替,将元素值

LeetCode刷题 1. Two Sum 两数之和 详解 C++语言实现 java语言实现

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, and you may not use the same element twice. Example: Given nums = [2, 7, 11, 15]

[LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树

Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target. Example 1: Input: 5 / 3 6 / \ 2 4 7 Target = 9 Output: True Example 2: Input: 5 / 3 6 / \ 2 4 7 Targe

LeetCode 167. Two Sum II - Input array is sorted (两数之和之二 - 输入的是有序数组)

Given an array of integers that is already sorted in ascending order, 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 m

167. Two Sum II - Input array is sorted两数之和

1. 原始题目 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值(index1 和 index2)不是从零开始的. 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素. 示例: 输入: numbers = [2, 7, 11, 15], target = 9 输出: [1,2] 解释: 2 与 7 之和等于目标数 9 .因此 in

给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X

题目:给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X 思路一: 1,先采用归并排序对这个数组排序, 2,然后寻找相邻<k,i>的两数之和sum,找到恰好sum>x的位置,如果sum=x则返回true, 3,找到位置后,保持i不变,从k处向前遍历,直到找到A[k]+A[i]等于x,并返回TRUE,如果找不到,则返回false. 论证步骤3:当前找到的位置恰好A[k]+A[i]>x,且前一位置的sum<x: 所以A[i]前面的数(不包括A[i])无论取哪两个数都

编程题:求两数之和

#include<stdio.h>       /*包含输入输出头文件*/ main()                            /*定义主函数*/ {  int a,b,sum;                /*定义整数变量a.b.sum*/ a=123;                        /*给a赋值*/ b=456;                        /*给b赋值*/ sum=a+b;                  /*令sum=a+b*/ p

leetcode刷题--两数之和(简单)

一.序言 第一次刷leetcode的题,之前从来没有刷题然后去面试的概念,直到临近秋招,或许是秋招结束的时候才有这个意识,原来面试是需要刷题的,面试问的问题都是千篇一律的,只要刷够了题就差不多了,当然你的基础也要扎实,毕竟在技术面的时候很容易露馅的. 所以奉劝各位还未毕业,在大三或大二的师弟师妹早点刷题,心里也有底气进入求职大军,毕竟大四开始刷题的话时间上有些太紧了,推荐刷题的话就是牛客和leetcode. 回归正题,这次记录的是leetcode刷的第一题--两数之和. 二.审题 审题真的很重要