Two SumLeetcode Problem 1

英文原题

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 = [2, 7, 11, 15], target = 9,
因为 nums[0] + nums[1] = 2 + 7 = 9
所以 需要返回 [0, 1]

问题签名

Java 代码

1 2 3 4 5
class Solution { public int[] twoSum(int[] nums, int target) { }}

Java Script 代码

1 2 3 4 5 6 7 8
/*** @param {number[]} nums* @param {number} target* @return {number[]}*/var twoSum = function(nums, target) { };

Python 代码

1 2
class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]:

ZZAX 微信公众

文档一更新,立刻告诉你