From fa73577ee2522a052b88dd42d619e4a46de2014d Mon Sep 17 00:00:00 2001 From: Jody Zhou <56443135+JodyZ0203@users.noreply.github.com> Date: Wed, 11 Nov 2020 19:32:32 -0500 Subject: [PATCH] =?UTF-8?q?TwoSum=20Python3=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...70\345\277\203\346\200\235\346\203\263.md" | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/twoSum\351\227\256\351\242\230\347\232\204\346\240\270\345\277\203\346\200\235\346\203\263.md" "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/twoSum\351\227\256\351\242\230\347\232\204\346\240\270\345\277\203\346\200\235\346\203\263.md" index 327fc23..8e15898 100644 --- "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/twoSum\351\227\256\351\242\230\347\232\204\346\240\270\345\277\203\346\200\235\346\203\263.md" +++ "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/twoSum\351\227\256\351\242\230\347\232\204\346\240\270\345\277\203\346\200\235\346\203\263.md" @@ -185,4 +185,29 @@ int[] twoSum(int[] nums, int target) {

-======其他语言代码====== \ No newline at end of file +======其他语言代码====== + +由[JodyZ203](https://github.com/JodyZ0203)提供 1. Two Sums Python3 解法代码: + +;; 只用一个哈希表 + +'''Python + +class Solution: + def twoSum(self, nums, target): + """ + :type nums: List[int] + :type target: int + :rtype: List[int] + """ + # 提前构造一个哈希表 + hashTable = {} + # 寻找两个目标数值 + for i, n in enumerate(nums): + other_num = target - n + # 如果存在这个余数 other_num + if other_num in hashTable.keys(): + # 查看是否存在哈希表里,如果存在的话就返回数组 + return [i, hashTable[other_num]] + # 如果不存在的话继续处理剩余的数 + hashTable[n] = i -- GitLab