Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md

This file was deleted.

80 changes: 80 additions & 0 deletions arrays.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Arrays

+ [Three sum (Generator version, O(n^3))](#three-sum-generator-version-on3)
+ [Three sum (Set + hash version O(n^2))](#three-sum-set--hash-version-on2)

## Three sum (Generator version, O(n^3))

https://leetcode.com/problems/3sum/

```python
class Solution(object):

def twoSum(self, nums, target, removed):
"""
:type nums: List[int]
:type target: int
:type removed: int
:rtype: List[int]
"""

for index, value in enumerate(nums):
if index == removed:
continue
if target - value in nums and index != nums.index(target - value) and removed != nums.index(target - value):
yield sorted([value, target - value])

def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""

triplets = []
for index, value in enumerate(nums):
pairs = []
for x in self.twoSum(nums, -value, index):
if x not in pairs:
pairs.append(x)
for p in pairs:
if sorted(p + [value]) not in triplets:
triplets += [sorted(p + [value])]
return triplets

```

## Three sum (Set + hash version O(n^2))

https://leetcode.com/problems/3sum/

```python
class Solution(object):

def twoSum(self, nums, target, removed):
"""
:type nums: List[int]
:type target: int
:type removed: set
:rtype: List[int]
"""

dict = {}
for index, value in enumerate(nums):
if target - value in dict:
removed.add((value, target - value, -target))
dict[value] = index
del dict

def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""

nums.sort()
out = set()
for index, value in enumerate(nums):
self.twoSum(nums[index + 1:], -value, out)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Зачем делать копии списков?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Про какие копии здесь идёт речь? если про return, то я изменил запись.

return [list(x) for x in out]

```