|
| 1 | + |
| 2 | +## 题目地址 |
| 3 | +https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/ |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +这是leetcode头号题目`two sum`的第二个版本,难度简单。 |
| 8 | + |
| 9 | +``` |
| 10 | +Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. |
| 11 | +
|
| 12 | +The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. |
| 13 | +
|
| 14 | +Note: |
| 15 | +
|
| 16 | +Your returned answers (both index1 and index2) are not zero-based. |
| 17 | +You may assume that each input would have exactly one solution and you may not use the same element twice. |
| 18 | +Example: |
| 19 | +
|
| 20 | +Input: numbers = [2,7,11,15], target = 9 |
| 21 | +Output: [1,2] |
| 22 | +Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2. |
| 23 | +
|
| 24 | +``` |
| 25 | + |
| 26 | +## 思路 |
| 27 | + |
| 28 | +由于题目没有对空间复杂度有求,用一个hashmap 存储已经访问过的数字即可。 |
| 29 | + |
| 30 | +假如题目空间复杂度有要求,由于数组是有序的,只需要双指针即可。一个left指针,一个right指针, |
| 31 | +如果left + right 值 大于target 则 right左移动, 否则left右移,代码比较简单, 不贴了。 |
| 32 | + |
| 33 | +> 如果数组无序,需要先排序(从这里也可以看出排序是多么重要的操作) |
| 34 | +
|
| 35 | + |
| 36 | +## 关键点解析 |
| 37 | + |
| 38 | +无 |
| 39 | + |
| 40 | + |
| 41 | +## 代码 |
| 42 | + |
| 43 | +```js |
| 44 | +/* |
| 45 | + * @lc app=leetcode id=167 lang=javascript |
| 46 | + * |
| 47 | + * [167] Two Sum II - Input array is sorted |
| 48 | + * |
| 49 | + * https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/ |
| 50 | + * |
| 51 | + * algorithms |
| 52 | + * Easy (49.46%) |
| 53 | + * Total Accepted: 221.8K |
| 54 | + * Total Submissions: 447K |
| 55 | + * Testcase Example: '[2,7,11,15]\n9' |
| 56 | + * |
| 57 | + * Given an array of integers that is already sorted in ascending order, find |
| 58 | + * two numbers such that they add up to a specific target number. |
| 59 | + * |
| 60 | + * The function twoSum should return indices of the two numbers such that they |
| 61 | + * add up to the target, where index1 must be less than index2. |
| 62 | + * |
| 63 | + * Note: |
| 64 | + * |
| 65 | + * |
| 66 | + * Your returned answers (both index1 and index2) are not zero-based. |
| 67 | + * You may assume that each input would have exactly one solution and you may |
| 68 | + * not use the same element twice. |
| 69 | + * |
| 70 | + * |
| 71 | + * Example: |
| 72 | + * |
| 73 | + * |
| 74 | + * Input: numbers = [2,7,11,15], target = 9 |
| 75 | + * Output: [1,2] |
| 76 | + * Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2. |
| 77 | + * |
| 78 | + */ |
| 79 | +/** |
| 80 | + * @param {number[]} numbers |
| 81 | + * @param {number} target |
| 82 | + * @return {number[]} |
| 83 | + */ |
| 84 | +var twoSum = function(numbers, target) { |
| 85 | + const visited = {} // 记录出现的数字, 空间复杂度N |
| 86 | + |
| 87 | + for (let index = 0; index < numbers.length; index++) { |
| 88 | + const element = numbers[index]; |
| 89 | + if (visited[target - element]) { |
| 90 | + return [visited[target - element], index + 1] |
| 91 | + } |
| 92 | + visited[element] = index + 1; |
| 93 | + } |
| 94 | + return []; |
| 95 | +}; |
| 96 | + |
| 97 | + |
| 98 | +``` |
| 99 | + |
| 100 | + |
| 101 | + |
0 commit comments