力扣刷题Day 21:两数之和(1)
1.题目描述
2.思路
暴力解法虽然不超时间限制,但是题解实在太妙了,哈希大法好!
3.代码(Python3)
class Solution:def twoSum(self, nums: List[int], target: int) -> List[int]:hash_table = dict()for i, num in enumerate(nums):if target - num in hash_table:return [hash_table[target - num], i]hash_table[nums[i]] = ireturn []
4.执行情况
5.感想
思维还是不够灵活,没想出判断target - num是否在哈希表中的妙计,只知道机械地判断两数之和,思维还是太僵直了。