From f2ba2c9e33eeeada55410111f6474cf5cd0636a1 Mon Sep 17 00:00:00 2001 From: tom4649 Date: Sat, 14 Mar 2026 09:54:38 +0900 Subject: [PATCH] 1. Two Sum --- 1/memo.md | 7 +++++++ 1/sol1.py | 11 +++++++++++ 1/sol2.py | 12 ++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 1/memo.md create mode 100644 1/sol1.py create mode 100644 1/sol2.py diff --git a/1/memo.md b/1/memo.md new file mode 100644 index 0000000..ffb99cd --- /dev/null +++ b/1/memo.md @@ -0,0 +1,7 @@ +# 1. Two Sum + +- [1. Two Sum](https://leetcode.com/problems/two-sum/description/) +- 愚直にsol1.pyを書いてしまった +- ハッシュを使うのが模範解答 + - https://discord.com/channels/1084280443945353267/1201211204547383386/1207251531041210408 + diff --git a/1/sol1.py b/1/sol1.py new file mode 100644 index 0000000..00b73ce --- /dev/null +++ b/1/sol1.py @@ -0,0 +1,11 @@ +class Solution(object): + def twoSum(self, nums, target): + """ + :type nums: List[int] + :type target: int + :rtype: List[int] + """ + for i in range(len(nums)): + for j in range(i + 1, len(nums)): + if nums[i] + nums[j] == target: + return [i, j] diff --git a/1/sol2.py b/1/sol2.py new file mode 100644 index 0000000..41f3133 --- /dev/null +++ b/1/sol2.py @@ -0,0 +1,12 @@ +class Solution(object): + def twoSum(self, nums, target): + """ + :type nums: List[int] + :type target: int + :rtype: List[int] + """ + hashmap = {} + for i, num in enumerate(nums): + if num in hashmap: + return [i, hashmap[num]] + hashmap[target - num] = i