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], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
题意:
给定数组nums和target,问数组中哪两个数的和等于target,输出这两个数的下标(保证答案唯一,并且两个数不同)
解决方案:
使用c++的map存每个数,将每个数映射到他所在的位置,然后for一遍,对于当前数(如x),如果map【target-x】不为0,那么答案就是map【x】,map【target-x】。
代码:
刚从acm转过来,不太适应交题风格。。。之后再补
本文探讨了经典的两数之和问题,即在一个整数数组中找到两个数,使它们的和等于特定的目标值。解决方案利用C++的map数据结构来高效地存储每个元素及其索引,从而快速查找配对元素。
685

被折叠的 条评论
为什么被折叠?



