We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 的那 两个 整数,并返回它们的数组下标。 你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。
示例 1:
输入:nums = [2,7,11,15], target = 9 输出:[0,1] 解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
示例 2:
输入:nums = [3,2,4], target = 6 输出:[1,2]
示例 3:
输入:nums = [3,3], target = 6 输出:[0,1]
The text was updated successfully, but these errors were encountered:
function twoSum(arr, target) { for (var i = 0, len = arr.length; i < len; i++) { for (var j = i + 1; j < len; j++) { if (arr[i] + arr[j] === target) return [i, j]; } } }
function twoSum(arr, target) { let memo = {}; for (var i = 0; i < arr.length; i++) { if (memo[target - arr[i]] === undefined) { memo[arr[i]] = i; } if (memo[target - arr[i]] !== undefined && memo[target - arr[i]] !== i) return [memo[target - arr[i]], i]; } }
Sorry, something went wrong.
No branches or pull requests
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。
示例 1:
示例 2:
示例 3:
The text was updated successfully, but these errors were encountered: