public class Solution
{
public int[] TwoSum(int[] nums, int target)
{
var ary = new int[];
for (int i = ; i < nums.Length; i++)
{
for (int j = i + ; j < nums.Length; j++)
{
if (nums[i] + nums[j] == target)
{
ary[] = i;
ary[] = j;
return ary;
}
}
}
return ary;
}
}
https://leetcode.com/problems/two-sum/#/description
补充一个python的实现:
class Solution:
def twoSum(self, nums: 'List[int]', target: 'int') -> 'List[int]':
s = {}
for i in range(len(nums)):
n = nums[i]
cop = target - n
if cop in s.keys():
return [s[cop],i]
s[nums[i]] = i
return []