From 69b2fc70605e3163f5f469b66ee057d596656a6f Mon Sep 17 00:00:00 2001 From: guyu Date: Mon, 12 Aug 2019 09:59:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20167.two-sum-ii-input-array-is-sorted=20?= =?UTF-8?q?add=20Python3=20implement=E2=80=A6=20(#101)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 167.two-sum-ii-input-array-is-sorted add Python3 implementation * Update 167.two-sum-ii-input-array-is-sorted.md --- .../167.two-sum-ii-input-array-is-sorted.md | 54 +++++++++++++------ 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/problems/167.two-sum-ii-input-array-is-sorted.md b/problems/167.two-sum-ii-input-array-is-sorted.md index b05c6aedd..945365519 100644 --- a/problems/167.two-sum-ii-input-array-is-sorted.md +++ b/problems/167.two-sum-ii-input-array-is-sorted.md @@ -28,7 +28,7 @@ Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2. 由于题目没有对空间复杂度有求,用一个hashmap 存储已经访问过的数字即可。 假如题目空间复杂度有要求,由于数组是有序的,只需要双指针即可。一个left指针,一个right指针, -如果left + right 值 大于target 则 right左移动, 否则left右移,代码比较简单, 不贴了。 +如果left + right 值 大于target 则 right左移动, 否则left右移,代码见下方python code。 > 如果数组无序,需要先排序(从这里也可以看出排序是多么重要的操作) @@ -40,6 +40,10 @@ Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2. ## 代码 +* 语言支持:JS,Python + +Javascript Code: + ```js /* * @lc app=leetcode id=167 lang=javascript @@ -56,25 +60,25 @@ Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2. * * 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. - * + * * 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. - * + * * Note: - * - * + * + * * Your returned answers (both index1 and index2) are not zero-based. * You may assume that each input would have exactly one solution and you may * not use the same element twice. - * - * + * + * * Example: - * - * + * + * * Input: numbers = [2,7,11,15], target = 9 * Output: [1,2] * Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2. - * + * */ /** * @param {number[]} numbers @@ -89,13 +93,33 @@ var twoSum = function(numbers, target) { if (visited[target - element] !== void 0) { return [visited[target - element], index + 1] } - visited[element] = index + 1; + visited[element] = index + 1; } return []; }; - - ``` - - +Python Code: + +```python +class Solution: + def twoSum(self, numbers: List[int], target: int) -> List[int]: + visited = {} + for index, number in enumerate(numbers): + if target - number in visited: + return [visited[target-number], index+1] + else: + visited[number] = index + 1 + +# 双指针思路实现 +class Solution: + def twoSum(self, numbers: List[int], target: int) -> List[int]: + left, right = 0, len(numbers) - 1 + while left < right: + if numbers[left] + numbers[right] < target: + left += 1 + if numbers[left] + numbers[right] > target: + right -= 1 + if numbers[left] + numbers[right] == target: + return [left+1, right+1] +```