051:Two Sum
题目描述: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.
tips:同时提供c++与python两种语言
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
for (int i = 0; i < nums.size(); i++)
{
for (int j = i + 1; j < nums.size(); j++)
{
if (nums == target - nums)
{
vector<int> lst1;
lst1.push_back(i);
lst1.push_back(j);
return lst1;
}
}
}
}
};
class Solution:
def twoSum(self, nums, target):
"""
:type nums: List
:type target: int
:rtype: List
"""
for index,num in enumerate(nums):
s=target-num
if s in nums and index!=nums.index(s):
return
return -1
页:
[1]