LeetCode 算法题【Easy】:1. Two Sum
Question
Given an array of integers nums
and an integer target
, return indices of the two numbers such that they add up to target
.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Example 1:
1 | Input: nums = [2,7,11,15], target = 9 |
Example 2:
1 | Input: nums = [3,2,4], target = 6 |
Example 3:
1 | Input: nums = [3,3], target = 6 |
My Answer
- 初始答案
1 | var twoSum = function(nums, target) { |
报错:
nums
: [0, 2, 3, 0]target
: 0- 没有考虑 target 和
nums[i]
一样大的情况
修改答案
1 | var twoSum = function(nums, target) { |
报错
nums
: [-1,-2,-3,-4,-5]target
: -8- 没有考虑负数的情况
修改答案
1 | var twoSum = function(nums, target) { |
My Answer’s Performance
Better Answer Reference
1 | var twoSum = function(nums, target) { |