일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- attribute
- Class
- concurrency
- Generator
- 43. Multiply Strings
- Regular Expression
- Substring with Concatenation of All Words
- kaggle
- 파이썬
- Protocol
- iterator
- 315. Count of Smaller Numbers After Self
- 109. Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- LeetCode
- Decorator
- 30. Substring with Concatenation of All Words
- Python Implementation
- 컴퓨터의 구조
- shiba
- 프로그래머스
- Python
- 시바견
- 밴픽
- Python Code
- t1
- 운영체제
- DWG
- 715. Range Module
- data science
Archives
- Today
- Total
Scribbling
LeetCode: 18. 4Sum 본문
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
ret = []
nums.sort()
previ = None
for i in range(len(nums)-3):
if nums[i] == previ:
continue
prevj = None
for j in range(i+1, len(nums)-2):
if nums[j] == prevj:
continue
preSum = nums[i] + nums[j]
l, r = j + 1, len(nums) - 1
while l < r:
summation = preSum + nums[l] + nums[r]
if summation < target:
l += 1
elif summation > target:
r -= 1
else:
ret.append([nums[i], nums[j], nums[l], nums[r]])
prevl, prevr = nums[l], nums[r]
while l <= r and nums[l] == prevl:
l += 1
while r >= l and nums[r] == prevr:
r -= 1
prevj = nums[j]
previ = nums[i]
return ret
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 22. Generate Parentheses (0) | 2021.08.24 |
---|---|
LeetCode: 19. Remove Nth Node From End of List (0) | 2021.08.22 |
LeetCode: 17. Letter Combinations of a Phone Number (0) | 2021.08.21 |
LeetCode: 16. 3Sum Closest (0) | 2021.08.21 |
LeetCode: 15. 3Sum (0) | 2021.08.19 |