-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd70330
commit 3e3a08e
Showing
1 changed file
with
23 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Solution: | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
""" | ||
Intuition: | ||
κΈ°μ‘΄μ νμλ 3sum λ¬Έμ μ μ μ¬νκ², | ||
ν΄μμ νμ¬ μ«μμ λν΄μ targetμ΄ λλ κ°μ μ°Ύλλ€. | ||
λ§μ½ μμ κ²½μ°, ν΄μμ νμ¬ κ°κ³Ό μΈλ±μ€λ₯Ό μ μ₯νλ€. | ||
Time Complexity: | ||
O(N): | ||
ν΄μλ μ κ·Όνλ λ°μ O(1)μ΄ μμλκ³ , | ||
μ΄ Nλ² λ°λ³΅ν΄μΌ νλ―λ‘ μκ°λ³΅μ‘λλ O(N)μ΄λ€. | ||
Space Complexity: | ||
O(N): | ||
μ΅μ μ κ²½μ° ν΄μμ Nκ°μ μ«μμ μΈλ±μ€λ₯Ό μ μ₯ν΄μΌ νλ€. | ||
""" | ||
complement_dict = {} | ||
for i, num in enumerate(nums): | ||
if target - num in complement_dict: | ||
return [complement_dict[target - num], i] | ||
else: | ||
complement_dict[num] = i |