Skip to content

1. 两数之和

1. 两数之和

给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 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]

代码

javascript
// 1. 两数之和
// https://leetcode.cn/problems/two-sum/description/

// 易理解,耗时长
export function twoSum(nums, target) {
  let result = []
  for(let i = 0, len = nums.length; i < len; i++) {
    const otherIndex = nums.indexOf(target - nums[i], i + 1)
    if (otherIndex > -1) {
      result = [i, otherIndex]
      break
    }
  }
  return result
};

// 遍历一遍,将值和索引保存在对象中
export function twoSumObj(nums, target) {
  let result = []
  const resObj = {}
  for(let i = 0, len = nums.length; i < len; i++) {
    if (nums[i] in resObj) {
      result = [resObj[nums[i]], i]
      break
    }
    resObj[target - nums[i]] = i
  }
  return result
};
typescript
// 1. 两数之和
// https://leetcode.cn/problems/two-sum/description/

// 易理解,耗时长
export function twoSum(nums: number[], target: number): number[] {
  let result: number[] = []
  for(let i = 0, len = nums.length; i < len; i++) {
    const otherIndex = nums.indexOf(target - nums[i], i + 1)
    if (otherIndex > -1) {
      result = [i, otherIndex]
      break
    }
  }
  return result
};

// 遍历一遍,将值和索引保存在对象中
export function twoSumObj(nums: number[], target: number): number[] {
  let result: number[] = []
  const resObj: { [propName: string]: number; } = {}
  for(let i = 0, len = nums.length; i < len; i++) {
    if (nums[i] in resObj) {
      result = [resObj[nums[i]], i]
      break
    }
    resObj[target - nums[i]] = i
  }
  return result
};

测试代码

ts
import { expect, test } from 'vitest'
import { twoSum, twoSumObj } from './typescript.ts'
import { twoSum as twoSumJs, twoSumObj as twoSumObjJs } from './javascript.js'

test(`twoSum([2, 7, 11, 15], 9) toEqual [0, 1]`, () => {
  expect(twoSum([2, 7, 11, 15], 9)).toEqual([0, 1])
})

test(`twoSum([3, 2, 4], 6) toEqual [1, 2]`, () => {
  expect(twoSum([3, 2, 4], 6)).toEqual([1, 2])
})

test(`twoSum([3, 3], 6) toEqual [0, 1]`, () => {
  expect(twoSum([3, 3], 6)).toEqual([0, 1])
})

test(`twoSumJs([2, 7, 11, 15], 9) toEqual [0, 1]`, () => {
  expect(twoSumJs([2, 7, 11, 15], 9)).toEqual([0, 1])
})

test(`twoSumJs([3, 2, 4], 6) toEqual [1, 2]`, () => {
  expect(twoSumJs([3, 2, 4], 6)).toEqual([1, 2])
})

test(`twoSumJs([3, 3], 6) toEqual [0, 1]`, () => {
  expect(twoSumJs([3, 3], 6)).toEqual([0, 1])
})

test(`twoSumObj([2, 7, 11, 15], 9) toEqual [0, 1]`, () => {
  expect(twoSumObj([2, 7, 11, 15], 9)).toEqual([0, 1])
})

test(`twoSumObj([3, 2, 4], 6) toEqual [1, 2]`, () => {
  expect(twoSumObj([3, 2, 4], 6)).toEqual([1, 2])
})

test(`twoSumObj([3, 3], 6) toEqual [0, 1]`, () => {
  expect(twoSumObj([3, 3], 6)).toEqual([0, 1])
})

test(`twoSumObjJs([2, 7, 11, 15], 9) toEqual [0, 1]`, () => {
  expect(twoSumObjJs([2, 7, 11, 15], 9)).toEqual([0, 1])
})

test(`twoSumObjJs([3, 2, 4], 6) toEqual [1, 2]`, () => {
  expect(twoSumObjJs([3, 2, 4], 6)).toEqual([1, 2])
})

test(`twoSumObjJs([3, 3], 6) toEqual [0, 1]`, () => {
  expect(twoSumObjJs([3, 3], 6)).toEqual([0, 1])
})