forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZetBe.py
More file actions
52 lines (43 loc) Β· 1.95 KB
/
ZetBe.py
File metadata and controls
52 lines (43 loc) Β· 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'''
λ¬Έμ : λ°°μ΄ μ€μμ λ μλ₯Ό λν΄μ νκ² λλ²κ° λλ μΈλ±μ€λ₯Ό λ°ννλΌ
ν μΈλ±μ€λ₯Ό λλ² μ¬μ©ν μ μμΌλ©°, λ΅μ νλλ§ μ‘΄μ¬νλ€.
νμ΄: λμ
λ리λ₯Ό μ΄μ©νμ¬ κ° μ«μμ μΈλ±μ€λ₯Ό μ μ₯ν ν,
νκ² λλ²μμ κ° μ«μλ₯Ό λΊ κ°μ΄ λμ
λ리μ μλμ§ νμΈ
κ·Έλ¦¬κ³ κ·Έ μ«μκ° λ λ² μ΄μ λ±μ₯νλμ§ νμΈ
(μ λ°μΌλ‘ λλμ΄ λ¨μ΄μ§λ κ²½μ° κ³ λ €)
μκ°λ³΅μ‘λ: O(n)
λμ
λ리μ κ° μ«μλ₯Ό μ μ₯νλλ° O(n), κ²ΉμΉμ§ μμ μλ₯Ό νμνλλ° μ΅μ
μ κ²½μ° O(n)
κ° μ«μμ λν΄ νκ² λλ²μμ λΊ κ°μ΄ λμ
λ리μ μλμ§ νμΈνλλ° O(1)μ΄λ―λ‘, μ 체 μκ°λ³΅μ‘λλ O(n)
곡κ°λ³΅μ‘λ: O(n)
λμ
λ리μ κ° μ«μλ₯Ό μ μ₯νλλ° O(n)μ 곡κ°μ΄ νμνλ―λ‘ μ 체 곡κ°λ³΅μ‘λλ O(n)
μ¬μ©ν μλ£κ΅¬μ‘°: λμ
λ리
μΌλ° 리μ€νΈλ₯Ό μ¬μ©νμ¬ νμν κ²½μ°, μκ°λ³΅μ‘λκ° O(n^2)μ΄ λ μ μλ€.
μ΅μ
μ κ²½μ°, n*(n-1)/2λ²μ νμμ΄ νμνλ°, μ΄λ κ²°κ΅ O(n^2)μ΄λ€.
'''
# μΆκ°λ‘ μ½λ리뷰λ₯Ό ν΅ν΄ κ°λ
μ± κ°μ ν μ½λ
'''
for i in range(len(nums)):
if target - nums[i] in d:
return [d[target-nums[i]][0], i]
if nums[i] not in d:
d[nums[i]] = [i]
else:
d[nums[i]] += [i]
'''
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
answ = []
d = {}
for i in range(len(nums)):
if nums[i] not in d:
d[nums[i]] = [i]
else:
d[nums[i]] += [i]
print(d)
for i in d.keys():
if target-i in d and len(d[target-i]) == 1 and target != i*2:
answ = [d[target-i][0], d[i][0]]
return answ
if target-i in d and len(d[target-i]) >= 2 and target == i*2:
answ = [d[target-i][0], d[i][1]]
return answ