首页 > 试题广场 > two-sum
[编程题]two-sum
  • 热度指数:36414 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
给出一个整数数组,请在数组中找出两个加起来等于目标值的数,
你给出的函数twoSum 需要返回这两个数字的下标(index1,index2),需要满足 index1 小于index2.。注意:下标是从1开始的
假设给出的数组中只存在唯一解
例如:

给出的数组为 {2, 7, 11, 15},目标值为9
输出 ndex1=1, index2=2



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

输入

[3,2,4],6

输出

[2,3]
头像 喝牛奶的牛
发表于 2020-03-10 19:39:14
题目描述: 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 h 展开全文
头像 Faiz_555
发表于 2019-08-31 00:42:23
注意下标从1开始class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int, int> m; 展开全文
头像 牛客413458805号
发表于 2020-06-30 16:29:50
链接:https://www.nowcoder.com/questionTerminal/20ef0972485e41019e39543e8e895b7f来源:牛客网 给出一个整数数组,请在数组中找出两个加起来等于目标值的数,你给出的函数twoSum 需要返回这两个数字的下标(index1,inde 展开全文